11332-summing Digits
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=2307
For a positive integer n, let f (n) denote the sum of the digits of n when represented in base 10. It is easy to sequence of numbers n, f (n), F (f (n)), F (f (n)), ... eventually becomes a single digit number that repeats forever. Let this single digit is denoted g (n).
For example, consider n = 1234567892. Then:
F (n) = 1+2+3+4+5+6+7+8+9+2 = 47
F (f (n)) = 4+7 = 11
F (f (f (n))) = 1+1 = 2
Therefore, g (1234567892) = 2.
This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45521.htm
Each line of input contains a single positive integer n at most 2,000,000,000. For each such integer, you are to output a single line containing g (n). The Input is terminated by n = 0 which should the not to be processed.
Sample input
2
11
47
1234567892
0
Output for sample input
2
2
2
2
Complete code:
/*0.016s*/
#include <cstdio>
#include <cstring>
char n[15];
int main ()
{
int i, SUM, Len;
while (gets (n), n[0]!= ' 0 ')
{
sum = 0;
Len = strlen (n);
for (i = 0; i < len; ++i)
sum + = N[i] &;
sum = SUM/10 + sum%;
sum = SUM/10 + sum%;
printf ("%d\n", sum);
}
return 0;
}