From c38db883a5c2268f6c44acea031d57264db9f5f5 Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Sun, 5 Dec 2021 02:57:58 -0500 Subject: Create task1.cpp --- Utils/SI_K1/task1.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Utils/SI_K1/task1.cpp (limited to 'Utils/SI_K1/task1.cpp') diff --git a/Utils/SI_K1/task1.cpp b/Utils/SI_K1/task1.cpp new file mode 100644 index 0000000..d06b550 --- /dev/null +++ b/Utils/SI_K1/task1.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; + +//Sem 4, task 6 +unsigned reverse(unsigned int n) +{ + unsigned int result = 0; + + while (n != 0) + { + int lastDigit = n % 10; + (result *= 10) += lastDigit; + n /= 10; + } + + return result; +} + +bool isSuffix(unsigned int n, unsigned int k) +{ + if (k == 0 && (n % 10 != 0)) + return false; + + while (k != 0) + { + int lastDigitOfN = n % 10; + int lastDigitOfK = k % 10; + + if (lastDigitOfN != lastDigitOfK) + return false; + + n /= 10; + k /= 10; + } + + return true; +} + +bool isPrefix(unsigned int n, unsigned int k) +{ + return isSuffix(reverse(n), reverse(k)); +} + +bool isInfix(unsigned int n, unsigned int k) +{ + if (n == 0 && k == 0) + return true; + + while (n > 0) + { + if (isSuffix(n, k)) + return true; + n /= 10; + } + return false; +} +///// end sem4 task6 + +//Solution + +bool task1(unsigned n, unsigned k) +{ + n = reverse(reverse(n/10) / 10); //remove the first and the last digit of n. + return isInfix(n,k); //call the function from the already solved task +} + + +int main() +{ + cout << task1(1234,12); +} -- cgit v1.3.1-23-gd82d