标签:
Description
Input
Output
Sample Input
Sample Output
1 #include<stdio.h> 2 #include<string.h> 3 int fun(int n) 4 { 5 int sum=0; 6 do 7 { 8 sum=0; 9 while(n>0) 10 { 11 sum+=n%10; 12 n/=10; 13 } 14 n=sum; 15 } 16 while(n>=10); 17 return sum; 18 } 19 int main() 20 { 21 int sum,i,len; 22 char s[1100]; 23 while(scanf("%s",s)) 24 { 25 if(strcmp(s,"0")==0) 26 break; 27 else 28 len=strlen(s); 29 sum=0; 30 for(i=0;i<len;i++) 31 sum+=s[i]-‘0‘; 32 while(sum>=10) 33 { 34 sum=fun(sum); 35 } 36 printf("%d\n",sum); 37 } 38 return 0; 39 }
标签:
原文地址:http://www.cnblogs.com/wangmengmeng/p/4552484.html