0
点赞
收藏
分享

微信扫一扫

Problem 36 Double-base palindromes (二进制回文)


Double-base palindromes


Problem 36

The decimal number, 585 = 10010010012 (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)


Answer:

872187

Completed on Sat, 29 Oct 2016, 15:02


代码:


#include <iostream>
#include <cstring>
#include <sstream>

using namespace std;
string dectobin(long long a) //十进制转换成二进制
{
stringstream bin;
while(a!=0)
{
bin<<a%2;
a=a/2;
}
string binary;
bin>>binary;
return binary;
}
bool isPalindrome(string a) //判断是否回文
{
for(int i=0;i<a.length();i++)
{
if(a[i]!=a[a.length()-1-i])
return false;
}
return true;
}
int main()
{
long long int sum=0;
for(int i=1;i<1000000;i+=2)
{
stringstream a;
a<<i;
if(isPalindrome(a.str()))
{
string binary=dectobin(i);
if(isPalindrome(binary))
{
sum+=i;
cout<<i<<" "<<binary<<endl;
}
}
}
cout<<sum;
return 0;
}




举报

相关推荐

二进制

玩转二进制

二进制求和

二进制基础

二进制文件

mysqldump二进制

缩短二进制

0 条评论