-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp36.cpp
More file actions
72 lines (65 loc) · 1.33 KB
/
Copy pathp36.cpp
File metadata and controls
72 lines (65 loc) · 1.33 KB
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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
using namespace std;
string binary( unsigned long n )
{
char result[ (sizeof( unsigned long ) * 8) + 1 ];
unsigned index = sizeof( unsigned long ) * 8;
result[ index ] = '\0';
do result[ --index ] = '0' + (n & 1);
while (n >>= 1);
return string( result + index );
}
string convertInt(int number)
{
if (number == 0)
return "0";
string temp="";
string returnvalue="";
while (number>0)
{
temp+=number%10+48;
number/=10;
}
for (int i=0;i<temp.length();i++)
returnvalue+=temp[temp.length()-i-1];
return returnvalue;
}
bool isPalindrome(string ps)
{
int l = ps.length()-1;
int i = 0;
while (i <= l)
{
if(ps.c_str()[i]!=ps.c_str()[l])
return false;
i++; l--;
}
return true;
}
int solve()
{
string d;
string b;
int count = 0;
for(int i = 1; i < 1000000; i++)
{
d = convertInt(i);
if (isPalindrome(d))
{
b = binary(i);
if (isPalindrome(b))
count += i;
}
}
cout << count << endl;
return count;
}
int main(int argc, char **argv)
{
solve();
return 0;
}