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.)
題意:輸出所有10進制、12進制、16進制各個位上的數字的和相等的數。例如:2992=1894(12進制)=A74(16進制),10進制:2+9+9+2=22,12進制:1+8+9+4=22,16進制:11+7+4=22。所以輸出2992。
代碼:
#include<stdio.h>
int main()
{
int a,b,c,j;
for(int i=1000;i<=9999;i++)
{
a=0,b=0,c=0;
j=i;
while(j)
{
a+=j%10;
j/=10;
}
j=i;
while(j)
{
b+=j%12;
j/=12;
}
j=i;
while(j)
{
c+=j%16;
j/=16;
}
if(a==b&&b==c)
printf("%d\n",i);
}
return 0;
}