Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Exams / Exam1 / prob1.cpp
diff options
context:
space:
mode:
1 files changed, 45 insertions, 0 deletions
-rw-r--r--Year 1/Programming introduction/Exams/Exam1/prob1.cpp45
diff --git a/Year 1/Programming introduction/Exams/Exam1/prob1.cpp b/Year 1/Programming introduction/Exams/Exam1/prob1.cpp
new file mode 100644
index 0000000..541c60c
--- /dev/null
+++ b/Year 1/Programming introduction/Exams/Exam1/prob1.cpp
@@ -0,0 +1,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;
+}