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/prob1.cpp | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Year 1/Programming introduction/Exams/Exam1/prob1.cpp (limited to 'Year 1/Programming introduction/Exams/Exam1/prob1.cpp') 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 + +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; +} -- cgit v1.3.1-26-gb76a