HDOJ 1197 Specialized Four-Digit Numbers 超详细

HDOJ 1197 Specialized Four-Digit Numbers

Problem Description

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 1893(12), 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

题意原文翻译是:查找并列出十进制表示法中的所有四位数字,这些数字的四位数字之和等于十六进制(以16为基数)表示法中的四位数字之和,也等于以十二进制(以12为基数)表示法中的四位数字之和。
例如,数字2991的(十进制)位数之和为2+9+9+1=21。因为2991=11728+8144+9*12+3,所以它的双数表示法是1893(12),这些数字加起来也是21。但是在十六进制中,2991是BAF16,并且11+10+15=36,所以程序应该拒绝2991。
但是,下一个数字(2992)在所有三种表示法(包括BB016)中的数字总和为22,因此2992应该在列出的输出中。(我们不希望小数位数少于四位(不包括前导零),因此2992是第一个正确答案。)

感觉这个题也没有多少可说的,多少进制就取余多少就能得到该进制的位数了,样例输出就那么多,不过我们程序写出来后能找到更多的所以样例只是一部分,不要把输出结果弄成和样例一样一样的,这道题会错的。样例只展示了一部分输出结果。

#include<stdio.h>

int f10(int num);
int f12(int num);
int f16(int num);//函数处理

int main(void)
{
    int i;
    for (i = 1000; i <= 9999; i++)
        if (f10(i) == f12(i) && f10(i) == f16(i) && f12(i) == f16(i))
        //判断是否符合条件
            printf("%d\n", i);
    return 0;
}

int f10(int num)
{
    int sum = 0;
    while (num)
    {
        sum += num % 10;
        num /= 10;
    }
    return sum;//返回各位数之和
}

int f12(int num)
{
    int sum = 0;
    while (num)
    {
        sum += num % 12;
        num /= 12;
    }
    return sum;
}

int f16(int num)
{
    int sum = 0;
    while (num)
    {
        sum += num % 16;
        num /= 16;
    }
    return sum;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值