zrq495
www.zrq495.com
Problem Description
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
 

 

Input
The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.
 

 

Output
For each integer in the input, output its digital root on a separate line of the output.
 

 

Sample Input
24 39 0
 

 

Sample Output
6 3
 
 1 #include<stdio.h>
2 #include<string.h>
3
4 int main()
5 {
6 int i, n, m, x;
7 char str[10000];
8 while(scanf("%s*c", str))
9 {
10 if ((str[0] == '0') && (str[1] == '\0'))
11 break;
12 n=strlen(str);
13 m=0;
14 for (i=0; i<n; i++)
15 {
16 m+=str[i]-'0';
17 }
18 while(m > 9)
19 {
20 x=m;
21 m=0;
22 while(x)
23 {
24 m+=x%10;
25 x/=10;
26 }
27 }
28 printf("%d\n", m);
29 }
30 return 0;
31 }
用字符串代替大数。
 
posted on 2012-02-26 10:26  zrq495  阅读(161)  评论(0编辑  收藏  举报