diff options
Added my solutions to todays programming introduction exam
1 files changed, 47 insertions, 0 deletions
| -rw-r--r-- | Year 1/Programming introduction/Exams/Exam1/prob2.cpp | 47 |
diff --git a/Year 1/Programming introduction/Exams/Exam1/prob2.cpp b/Year 1/Programming introduction/Exams/Exam1/prob2.cpp new file mode 100644 index 0000000..c941112 --- /dev/null +++ b/Year 1/Programming introduction/Exams/Exam1/prob2.cpp @@ -0,0 +1,47 @@ +#include <iostream> + +using namespace std; + +bool isNeighbourNum(int n) { + while (n != 0) { + if (n % 10 == (n / 10) % 10) + return false; + n /= 10; + } + return true; +} + +int subNeighbours(int a, int b) { + int smallest = 0, biggest = 0; + + for (int i = a; i <= b; i++) { + if (isNeighbourNum(i)) { + if (i < smallest || !smallest) + smallest = i; + if (i > biggest || !biggest) + biggest = i; + } + } + + if (!smallest || !biggest) { + return -1; + } + + return biggest - smallest; +} + +int main() { + cout << "a = "; + int a; + cin >> a; + cout << "b = "; + int b; + cin >> b; + + int sub = subNeighbours(a, b); + + if (sub == -1) + cout << "No adjacent numbers!" << endl; + else + cout << sub << endl; +} |
