Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Exams / Exam1 / prob2.cpp
blob: c9411121316c819771bdf2e21a2f1659f2de609e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
}