题目链接:https://projecteuler.net/problem=36
还是暴力,不考虑复杂度的话,string判断回文串可能写的会快一点
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=25;
bool judge10(int x)
{
string s;
while(x)
{
s+=(x%10)+'0';
x/=10;
}
string rev=s;
reverse(s.begin(),s.end());
return s==rev;
}
bool judge2(int x)
{
string s;
while(x)
{
s+=(x%2)+'0';
x/=2;
}
string rev=s;
reverse(s.begin(),s.end());
return s==rev;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int ans=0;
for(int i=1;i<=1000000;i++)
{
if(judge10(i)&&judge2(i))
ans+=i;
}
printf("%d\n",ans);
return 0;
}