首页 > 其他 > 详细

Project Euler:Problem 36 Double-base palindromes

时间:2015-06-04 11:57:59      阅读:153      评论:0      收藏:0      [点我收藏+]

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.)


#include <iostream>
#include <string>
using namespace std;

string bin(int a)
{
	string res = "";
	while (a)
	{
		if (a % 2 == 1)
			res = '1' + res;
		else
			res = '0' + res;
		a /= 2;
	}
	return res;
}

bool pali_str(string  s)
{
	for (int i = 0; i < s.length() / 2; i++)
	{
		if (s[i] != s[s.length() - 1 - i])
			return false;
	}
	return true;
}

bool pali_int(int a)
{
	string s = "";
	while (a)
	{
		char c = a % 10 + '0';
		a /= 10;
		s = c + s;
	}
	if (pali_str(s))
		return true;
	else
		return false;
}



int main()
{
	int res = 0;
	for (int i = 1; i <= 1000000; i++)
	{
		if (pali_int(i) && pali_str(bin(i)))
			res += i;
	}
	cout << res << endl;
	system("pause");
	return 0;
}


Project Euler:Problem 36 Double-base palindromes

原文:http://blog.csdn.net/youb11/article/details/46356413

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!