HDU Sky数 2097

解题思路:类比求出10进制数各个位上的数字之和,求出12进制和16进制上的数。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int n;
 6 
 7 int getShi(int t)
 8 {
 9     int ans = 0;
10     t = n;
11     while(t)
12     {
13         ans += t%10; //草稿纸上演示一下就知道原理。
14         t /= 10;
15     }
16     //printf("ans10 = %d
", ans); //打印出来,确认一下。
17     return ans;
18 }
19 
20 int getShiliu(int t)
21 {
22     int ans = 0;
23     t = n;
24     while(t)
25     {
26         ans += t%16;
27         t /= 16;
28     }
29     //printf("ans16 = %d
", ans);
30     return ans;
31 }
32 
33 int getShier(int  t)
34 {
35     int ans = 0;
36     t = n;
37     while(t)
38     {
39         ans += t%12;
40         t /= 12;
41     }
42     //printf("ans12 = %d
", ans);
43     return ans;
44 }
45 
46 int main()
47 {
48     int a, b, c;
49     while(~scanf("%d", &n) && n)
50     {
51         a = getShi(n), b = getShiliu(n), c = getShier(n);
52         if(a == b && b == c) //刚开始,直接用a==b==c判断,是不对的,原因自己思考。
53             printf("%d is a Sky Number.
", n);
54         else printf("%d is not a Sky Number.
", n);
55     }
56     return 0;
57 }
View Code
原文地址:https://www.cnblogs.com/loveprincess/p/4817678.html