-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler55.cpp
83 lines (63 loc) · 1.07 KB
/
euler55.cpp
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
74
75
76
77
78
79
80
81
82
83
//DESCRIPTION: Brute force approach.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//generates the next number in the sequence
string nextit(string a)
{
string rev = a;
reverse(rev.begin(), rev.end());
string result;
char d1, d2, r;
int c = 0;
for (int i = rev.size() - 1; i >= 0; i--)
{
d1 = rev[i] - '0';
d2 = a[i] - '0';
r = d1 + d2 + c;
if (r >= 10)
{
r = r - 10 + '0';
c = 1;
}
else
{
r = r + '0';
c = 0;
}
result.push_back(r);
}
if (c == 1)
result.push_back('1');
reverse(result.begin(), result.end());
return result;
}
bool il(string a)
{
string current = a;
string rev;
for (int i = 0; i < 49; i++)
{
current = nextit(current);
rev = current;
reverse(rev.begin(), rev.end());
if (current.compare(rev) == 0)
return false;
}
return true;
}
int main()
{
int count = 0;
for (int i = 1; i < 10000; i++)
{
if (il(to_string(i)))
{
count++;
}
}
cout << count;
cin.get();
return 0;
}