forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.cpp
More file actions
51 lines (44 loc) · 915 Bytes
/
36.cpp
File metadata and controls
51 lines (44 loc) · 915 Bytes
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
/**
* Problem: Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2.
* Algorithm: Brute Force
* Author: daiwb
* Date: 2008/08/31
*/
#include <iostream>
#include <sstream>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
typedef long long LL;
bool isPal(string str) {
int len = str.length();
REP(i,len >> 1) {
if (str[i] != str[len - 1 - i]) return false;
}
return true;
}
bool isPalDec(int val) {
stringstream ss;
ss << val;
string str;
ss >> str;
return isPal(str);
}
bool isPalBin(int val) {
string str = "";
do {
str += char((val & 1) + '0');
val >>= 1;
} while (val != 0);
return isPal(str);
}
void run() {
int res = 0;
for (int i = 1; i < 1000000; i += 2) {
if (isPalDec(i) && isPalBin(i)) res += i;
}
cout << res << endl;
}
int main() {
run();
}