#include<stdio.h>
#include<string.h>
#define MAX 10000
char ch[MAX];
int main()
{
while(gets(ch))
{
if(strcmp(ch,"0")==0)
break;
int j=strlen(ch),i,s1=0,k,s2=0;
for(i=0; i<j; i++)
s1=s1+ch[i]-‘0‘;
while(1)
{
while(s1!=0)
{
s2=s2+s1%10;
s1=s1/10;
}
if(s2<10)
break;
else
{
s1=s2;
s2=0;
}
}
printf("%d\n",s2);
}
return 0;
}
原文:http://www.cnblogs.com/-lgh/p/4992672.html