首页 > 代码库 > hdu1013 Digital Roots

hdu1013 Digital Roots

思路:

九余数定理。

http://blog.csdn.net/asd7f7/article/details/53994666

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long ll;
 6 
 7 char a[1005];
 8 
 9 int main()
10 {
11     int n;
12     while (true)
13     {
14         scanf("%s", a);
15         if (a[0] == 0)
16             break;
17         int l = strlen(a);
18         int ans = 0;
19         for (int i = 0; i < l; i++)
20         {
21             ans += (a[i] - 0);
22             ans %= 9;
23         }
24         printf("%d\n", !ans ? 9 : ans);
25     }
26     return 0;
27 }

 

hdu1013 Digital Roots