/*1115题:求一个正数的digital root*/
#include <stdio.h>
#include <string.h>
/*得到初始值,因为整数可能很大!*/
int getinitsum(const char* line)
{
int sum=0;
char *s=line;
while(*s)
{
sum+=*s-'0';
s++;
}
return sum;
}
/*求n的数位和*/
int getsum(int n)
{
int sum=0;
while(n)
{
sum+=n%10;
n/=10;
}
return sum;
}
/*求n的digital root*/
int getroot(int n)
{
int sum=getsum(n);
while(sum>=10)
{
sum=getsum(sum);
}
return sum;
}
int main()
{
int n, root;
char line[1024];
while(scanf("%s", line)!=EOF && strcmp(line,"0")!=0)
{
n=getinitsum(line);
root=getroot(n);
printf("%d\n",root);
}
return 0;
}
转载于:https://blog.51cto.com/1010134430/1630870