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

题目大意:

十进制数字585 = 10010010012 (二进制),可以看出在十进制和二进制下都是回文(从左向右读和从右向左读都一样)。

求100万以下所有在十进制和二进制下都是回文的数字之和。

(注意在两种进制下的数字都不包括最前面的0)

//(Problem 36)Double-base palindromes
// Completed on Thu, 31 Oct 2013, 13:12
// Language: C
//
// 版权所有(C)acutus (mail: acutus@126.com)
// 博客地址:http://www.cnblogs.com/acutus/
#include<stdio.h>
#include<stdbool.h> bool test(int *a, int n)
{
bool flag = true;
for(int i = ; i < n/; i++) {
if(a[i] != a[n-i-]) {
flag = false;
break;
}
}
return flag;
} bool palindromes(int n, int base) //判断整数n在基为base时是否为回文数
{
int a[];
int i = ;
while(n) {
a[i++] = n % base;
n /= base;
}
return test(a,i);
} int main(void)
{
int sum = ;
for(int i = ; i <= ; i += )
{
if(palindromes(i, ) && palindromes(i, ))
sum += i;
}
printf("%d\n", sum);
return ;
}
Answer:
872187

(Problem 36)Double-base palindromes的更多相关文章

  1. (Problem 42)Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  2. (Problem 70)Totient permutation

    Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...

  3. (Problem 29)Distinct powers

    Consider all integer combinations ofabfor 2a5 and 2b5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, ...

  4. (Problem 73)Counting fractions in a range

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  5. (Problem 41)Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  6. (Problem 74)Digit factorial chains

    The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...

  7. (Problem 46)Goldbach's other conjecture

    It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...

  8. (Problem 72)Counting fractions

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  9. (Problem 53)Combinatoric selections

    There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...

随机推荐

  1. HDU 1012 u Calculate e

    题解:直接模拟 #include <cstdio> int main(){ puts("n e");puts("- -----------");pu ...

  2. 类型萃取(type traits)

    1. 类型萃取的作用 类型萃取使用模板技术来萃取类型(包含自定义类型和内置类型)的某些特性,用以判断该类型是否含有某些特性,从而在泛型算法中来对该类型进行特殊的处理用来提高效率或者其他.例如:在STL ...

  3. 动态链接库 DLL

    动态链接库DLL 不使用时不会有任何作用,只有在其他模块调用动态链接库中的函数时,它才发挥作用. 一.静态库与动态库 1.静态库 函数和数据被编译进一个二进制文件(.LIB),编译时,会将其组合起来创 ...

  4. 黑客瑞士军刀NC使用教程

    ###################################################################### 1. 写在前面的话 ################### ...

  5. poj 2480 (欧拉函数应用)

    点击打开链接 //求SUM(gcd(i,n), 1<=i<=n) /* g(n)=gcd(i,n),根据积性定义g(mn)=g(m)*g(n)(gcd(m,n)==1) 所以gcd(i,n ...

  6. 七日筑基——C#第二天

    上一次讲到了变量,变量这个东西可以说是编程的基础,主要的作用就是用来存放数据,就跟做菜一样的,不同的菜要放在不同类型的容器中,那么不同的数据也需要存放在不同类型的变量里.先放张饭菜的图给大家看看,增加 ...

  7. iOS实践02

    第二天了,上了一天课,软件测试.数据挖掘.概率论,晚上了才有时间捣鼓捣鼓程序. 今天只是简单的做了一点.觉得自己思考的写不出来,只能简单的写一个过程,不像第一次写这个,少了很多思考的. 1.完善tab ...

  8. UVA 10798 - Be wary of Roses (bfs+hash)

    10798 - Be wary of Roses You've always been proud of your prize rose garden. However, some jealous f ...

  9. JS数组方法总结

    数组的常用方法总结   不改变原数组 1.Array.length;                       //获取数组长度 2.Array.join();                   ...

  10. python实现进度条

    先说一下文本系统的控制符: \r: 将光标移动到当前行的首位而不换行: \n: 将光标移动到下一行,并不移动到首位: \r\n: 将光标移动到下一行首位. 环境: root@ubuntu16:/ale ...