开发者社区> 技术小甜> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

zoj 2406 Specialized Four-Digit Numbers

简介:
+关注继续查看
Specialized Four-Digit Numbers
Time Limit: 2 Seconds      Memory Limit: 65536 KB

Find and list all four-digit numbers in decimal notation that have the property that the sum of its four digits equals the sum of its digits when represented in hexadecimal (base 16) notation and also equals the sum of its digits when represented in duodecimal (base 12) notation.

For example, the number 2991 has the sum of (decimal) digits 2+9+9+1 = 21. Since 2991 = 1*1728 + 8*144 + 9*12 + 3, its duodecimal representation is 189312, and these digits also sum up to 21. But in hexadecimal 2991 is BAF16, and 11+10+15 = 36, so 2991 should be rejected by your program.

The next number (2992), however, has digits that sum to 22 in all three representations (including BB016), so 2992 should be on the listed output. (We don't want decimal numbers with fewer than four digits - excluding leading zeroes - so that 2992 is the first correct answer.)

 

Input

There is no input for this problem.

 

Output

Your output is to be 2992 and all larger four-digit numbers that satisfy the requirements (in strictly increasing order), each on a separate line with no leading or trailing blanks, ending with a new-line character. There are to be no blank lines in the output. The first few lines of the output are shown below.

 

Sample Input

There is no input for this problem.

 

Sample Output

2992
2993
2994
2995
2996
2997
2998
2999

分析:

(1)题意很简单,就是求各种进制下数字之和相等的四位数。16进制和12进制求解过程就是分解成因子的形式计算,先求出能组成这些数字的因子的所有情况,然后组合求出每个因子的系数返回相加之和即可。

复制代码
#include <stdio.h>

int i;
int base_duo[4] = {1,12,144,1728};
int base_hex[4] = {1,16,256,4096};

//得到12进制的数字之和
int get_sum_duo(int num)
{
    int ex[4]={0},sum=0;
    for(i=3;i>=0;i--)
    {

        if(num>=base_duo[i])
        {
            num -= base_duo[i];
            ex[i]++;
            i++;
        }
    }
    for(i=0;i<4;i++)
    sum+=ex[i];
    return sum;
}

//得到16进制的数字之和
int get_sum_hex(int num)
{
    int ex[4]={0},sum=0;
    for(i=3;i>=0;i--)
    {
        if(num>=base_hex[i])
        {
            num -= base_hex[i];
            ex[i]++;
            i++;
        }
    }
    for(i=0;i<4;i++)
    sum+=ex[i];
    return sum;
}
//得到10进制之和
int get_sum(int num)
{
    int sum=0;
    while(num>0)
    {
        sum += num%10;
        num /=10;
    }
    return sum;
}

int main()
{
    int j,tem;
    for(j=2992;j<10000;j++)
    {
        tem = get_sum(j);
        if(get_sum_duo(j)==tem&&get_sum_hex(j)==tem)
        printf("%d\n",j);
    }
    return 0;
}
复制代码

 









本文转自NewPanderKing51CTO博客,原文链接:http://www.cnblogs.com/newpanderking/archive/2012/10/05/2712299.html ,如需转载请自行联系原作者


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
HDU-1061,Rightmost Digit(快速幂)
HDU-1061,Rightmost Digit(快速幂)
0 0
poj 2406 Power Strings【KMP】
点击打开题目 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33548   Accepted: 13935 Description ...
506 0
zoj 3690 Choosing number
点击打开zoj 3690 思路: 递推+矩阵快速幂 分析; 1 题目的意思是有n个人和m个数和一个k,现在每个人可以选择一个数,但是要求如果相邻的两个人选择相同的数,那么这个数要大于k 2 假设F(n)表示前n个人第n个人选择的数大于k的...
566 0
poj 1150 The Last Non-zero Digit
点击打开链接poj 1150 思路:组合数学+排列组合 分析: 1 求排列数A(n , m)的末尾第一个非0数。 2 总结一下,求n!最后一个非0位的步骤如下: step1:首先将n!中所有2,5因子去掉;step2:然后求出剩下的一串数字相乘后末尾的那个数。
538 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载