Project Euler problem 36
2012-11-05 15:49
393 查看
一百万以内的回文数不超过2千个。
如果直接遍历1到100W显然有点慢
枚举每一位的数字然后生成回文数就快多了
然后再判断是不是二进制回文。
如果直接遍历1到100W显然有点慢
枚举每一位的数字然后生成回文数就快多了
然后再判断是不是二进制回文。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <set> #include <stack> #include <cmath> #include <map> #include <ctime> #define MAXN 111111 #define INF 100000007 using namespace std; int a[1000001]; int cnt; bool gao(int x) { string tmp1 = "", tmp2 = ""; while(x) { tmp1 = tmp1 + char(x % 2 + '0'); tmp2 = char(x % 2 + '0') + tmp2; x /= 2; } if(tmp1 == tmp2) return 1; return 0; } int main() { for(int i = 1; i <= 9; i++) a[cnt++] = i, a[cnt++] = i * 10 + i; for(int i = 1; i <= 9; i++) for(int j = 0; j <= 9; j++) a[cnt++] = i * 100 + j * 10 + i, a[cnt++] = i * 1000 + j * 100 + j * 10 + i; for(int i = 1; i <= 9; i++) for(int j = 0; j <= 9; j++) for(int k = 0; k <= 9; k++) a[cnt++] = i * 10000 + j * 1000 + k * 100 + j * 10 + i, a[cnt++] = i * 100000 + j * 10000 + k * 1000 + k * 100 + j * 10 + i; int ans = 0; for(int i = 0; i < cnt; i++) { if(gao(a[i])) ans += a[i]; } cout << ans << endl; return 0; }
相关文章推荐
- http://projecteuler.net/problem=36 [Answer:872187]
- project euler Problem 18 & 36
- Project Euler Problem 36 Double-base palindromes
- project euler Problem 18 & 36
- project euler Problem 36
- project euler Problem 36
- Project Euler Problem 36
- Project Euler Problem 32
- Project Euler Problem 81-90
- project euler Problem 15
- Project Euler Problem 76-整数分拆
- http://projecteuler.net/problem=13 [Answer:5537376230]
- http://projecteuler.net/problem=5【求解数组所有元素的最小公倍数】
- http://projecteuler.net/problem=39 [Answer:840]
- Project Euler -> problem 12
- project euler problem 5:Smallest multiple
- Project Euler Problem 25 :1000-digit Fibonacci number
- Project Euler problem 8
- Project Euler problem 17
- Project Euler problem 27