From 0139d20b8844b4f1946c84b07e567faf5ba618c7 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 23 Nov 2021 16:59:41 +0200 Subject: Added my solutions to todays programming introduction exam --- .../Programming introduction/Exams/Exam1/prob2.cpp | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Year 1/Programming introduction/Exams/Exam1/prob2.cpp (limited to 'Year 1/Programming introduction/Exams/Exam1/prob2.cpp') 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 + +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; +} -- cgit v1.3.1-26-gb76a