-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem036.cpp
62 lines (57 loc) · 1.07 KB
/
problem036.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
#include <iostream>
#include <vector>
#include <sstream>
#include <math.h>
using namespace std;
string toStr(int number)
{
stringstream ss;
ss << number;
return ss.str();
}
bool tenPalindrome(int number)
{
string num = toStr(number);
string rnum = string(num.rbegin(), num.rend());
return num == rnum;
}
bool twoPalindrome(int number)
{
int numb = number;
vector<int> powers;
string num = "";
for (int i = 0; i <= 20; i++)
{
powers.push_back(pow(2, i));
}
for (int i = powers.size() - 1; i >= 0; i--)
{
if (numb >= powers[i])
{
num += "1";
numb -= powers[i];
}
else if (num != "")
{
num += "0";
}
}
if (num == "")
{
return false;
}
string rnum = string(num.rbegin(), num.rend());
return num == rnum;
}
int main()
{
int total = 0;
for (int i = 1; i < 1000000; i++)
{
if (tenPalindrome(i) && twoPalindrome(i))
{
total += i;
}
}
cout << total << endl;
}