Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Exams / Exam1 / prob1.cpp
diff options
context:
space:
mode:
Commit0139d20b8844b4f1946c84b07e567faf5ba618c7 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-23 16:59:41 +0200
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2021-11-23 16:59:41 +0200
Filesd2d06058ccce1750c0a1cdc0ac590eac53487010 /Year 1/Programming introduction/Exams/Exam1/prob1.cpp
uni-projects-0139d20b8844b4f1946c84b07e567faf5ba618c7.tar
uni-projects-0139d20b8844b4f1946c84b07e567faf5ba618c7.tar.gz
uni-projects-0139d20b8844b4f1946c84b07e567faf5ba618c7.zip
Parent7a59e1811ccc5f2ff2d91647434a34900ca23f82 (diff)
Added my solutions to todays programming introduction exam
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;
+}