(Problem 36)Double-base palindromes

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 = 0; i < n/2; i++) {
        if(a[i] != a[n-i-1]) {
            flag = false;
            break;
        }
    }
    return flag;
}

bool palindromes(int n, int base)  //判断整数n在基为base时是否为回文数
{
    int a[100];
    int i = 0;
    while(n) {
        a[i++] = n % base;
        n /= base;
    }
    return test(a,i);
}

int main(void)
{
    int sum = 0;
    for(int i = 1; i <= 1000000; i += 2)
    {
        if(palindromes(i, 10) && palindromes(i, 2))
            sum += i;
    }
    printf("%d\n", sum);
    return 0;
}
Answer:
872187

转载于:https://www.cnblogs.com/acutus/p/3547513.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值