HDU Digital Roots(简单题 有坑)

这道题的坑就在于测试数据大小题目中并没有要求,long long都不行,只能用字符串去存输入的数据。

 1 #include <string>
 2 #include <cstdio>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int resolve(int n)
 7 {
 8     if (n < 10)
 9     {
10         return n;
11     }
12     else
13     {
14         int res = 0;
15         while (n != 0)
16         {
17             res += (n % 10);
18             n /= 10;
19             if (n == 0 && res >= 10)
20             {
21                 n = res;
22                 res = 0;
23             }
24         }
25         return res;
26     }
27 }
28 
29 int main(void)
30 {
31     string str;
32     while (cin>>str && str[0]!='0')
33     {
34         int len = str.length();
35         int res = 0;
36         int sum = 0;
37         for (int i = 0; i < len; i++)
38         {
39             res += str[i] - '0';
40         }
41 //合九法;一个数的数字根等于这个数模9,也等于各个位所有数之和模9
42 //        res = (res % 9) ? (res % 9) : 9;
43         res = resolve(res);
44         printf("%d\n", res);
45     }
46     return 0;
47 }

 

posted @ 2018-07-26 10:12  Feng_Change  阅读(150)  评论(0编辑  收藏  举报