9度教程第42题

九度教程第42题

题目地址:http://ac.jobdu.com/problem.php?cid=1040&pid=41

C语言源码:

#include<stdio.h>
int root(int n)
{
	int num=0;
	while(n)
	{
		num+=n%10;
		n/=10;
	}
	return num;
}
int main()
{
	int n,i;
	char s[1000];
	scanf("%s",s);
	getchar();
	while(s[0]!='0')
	{
		i=0;
		n=0;
		while(s[i]!='\0')
			n+=s[i++]-'0';
		while(n>=10)
			n=root(n);
		printf("%d\n",n);
		scanf("%s",s);
		getchar();
	}
}