您的位置:首页 > 其它

UVa 11332 Summing Digits (water ver.)

2013-11-07 19:21 344 查看

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 see that the sequence of numbers
n, f(n), f(f(n)), f(f(f(n))), ...
eventually becomes a single digit number that repeats forever. Let this single digit be 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
.

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)
. Input is terminated by
n = 0
which should not be processed.

Sample input

2
11
47
1234567892
0

Output for sample input

2
2
2
2


完整代码:

/*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] & 15;
		sum = sum / 10 + sum % 10;
		sum = sum / 10 + sum % 10;
		printf("%d\n", sum);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: