Sky数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18379 Accepted Submission(s): 10427
Problem Description
Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22,啊哈,真是巧啊。Sky非常喜欢这种四位数,由于他的发现,所以这里我们命名其为Sky数。但是要判断这样的数还是有点麻烦啊,那么现在请你帮忙来判断任何一个十进制的四位数,是不是Sky数吧。
Input
输入含有一些四位正整数,如果为0,则输入结束。
Output
若n为Sky数,则输出“#n is a Sky Number.”,否则输出“#n is not a Sky Number.”。每个结果占一行。注意:#n表示所读入的n值。
Sample Input
2992 1234 0
Sample Output
2992 is a Sky Number. 1234 is not a Sky Number.
#include
int main(){
int a, b, c, d, i, sum1, sum2, sum3;
while (scanf("%d", &a), a){
b = a;
c = a;
d = a;
sum1 = 0;
sum2 = 0;
sum3 = 0;
for (i = 0; i < 36; i++){
sum1 = sum1 + b % 16;
b = b / 16;
if (b == 0)
break;
}
for (i = 0; i < 36; i++){
sum2 = sum2 + c % 12;
c = c / 12;
if (c == 0)
break;
}
for (i = 0; i < 36; i++){
sum3 = sum3 + d % 10;
d = d / 10;
if (d == 0)
break;
}
if (sum1 == sum3 && sum2 == sum3)
printf("%d is a Sky Number.\n", a);
else
printf("%d is not a Sky Number.\n", a);
}
return 0;
}
OR
#include
int Sum(int n, int as)
{
int sum = 0;
while (n)
{
sum += n % as;
n /= as;
}
return sum;
}
int main(void)
{
int n;
while (scanf("%d", &n), n)
printf(Sum(n,10)==Sum(n,12)&&Sum(n,10)==Sum(n,16)?"%d is a Sky Number.\n":"%d is not a Sky Number.\n", n);
return 0;
}