blob: 544b7f690441195cf05a943c31907de7e328ec94 (
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#include <iostream>
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);
}
|