Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Exams / Exam1 / prob1.cpp
blob: 541c60c0f0e1976d853399227a0fdaa94492674b (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
#include <iostream>

using namespace std;

bool canConcatNumber(unsigned int N, unsigned int K) {
	if (N < 100) return false;

	/* Get middle digits (which are reversed) */

	unsigned int middleN = 1; // using 1 for cases where we have zeroes between our end digits, like in 1002001
	for (unsigned int i = 0; N != 0; i++) {
		if (i > 0 && N > 9)
			(middleN *= 10) += N % 10;
		N /= 10;
	}

	/* Get width of K as a power of 10 and reverse K */

	unsigned int kWidth = 1, revK = 0;
	for (unsigned int tmpK = K; tmpK != 0; kWidth *= 10) {
		(revK *= 10) += tmpK % 10;
		tmpK /= 10;
	}

	/* Check if revK is inside middleN */

	while (middleN != 1) {
		if ((middleN - revK) % kWidth == 0)
			return true;
		middleN /= 10;
	}

	return false;
}

int main() {
	cout << "N = ";
	int N;
	cin >> N;
	cout << "K = ";
	int K;
	cin >> K;

	cout << (canConcatNumber(N, K) ? "True" : "False") << endl;
}