题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2097
直接看ac代码吧,上面有注释解释
ac代码:
#include<stdio.h>
int num[9005]={0};
int judge(int a){
int decsum = 0;//十进制的数位和
int dousum = 0;//十二进制数位和
int hexsum = 0;//十六进制数位和
int temp = a;
while(a){//算出十进制的数位和
decsum += a % 10;
a = a / 10;
}
a = temp;
while(a){//算出十二进制数位和
dousum += a % 12;
a = a / 12;
}
a = temp;
while(a){//算出十六进制数位和
hexsum += a % 16;
a = a / 16;
}
if(decsum==dousum&&decsum==hexsum){
return 1;
}
return 0;
}
int main(){
int n = 0;
for(int i = 1000;i < 10000;i++){
if(judge(i) == 1){
num[i-1000] = 1;
}
}
while(scanf("%d",&n) != EOF){
if(n == 0)break;
if(num[n-1000]==1){
printf("%d is a Sky Number.\n",n);
}else{
printf("%d is not a Sky Number.\n",n);
}
}
return 0;
}