HDU 1013不解释
View Code
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 char s[1001]; 6 int Roots(int x) 7 { 8 int num=0,i; 9 while( x ) 10 { 11 num += x%10; 12 x /= 10; 13 } 14 if(num < 10) 15 return num; 16 else 17 return Roots(num); 18 } 19 20 int main() 21 { 22 int sum,i, ans; 23 24 while(scanf("%s",s) != EOF) 25 { 26 if(s[0] == '0') break; 27 sum = 0; 28 for(i=0; i<strlen(s); i++) 29 sum += (s[i]-'0'); 30 if(sum < 10) 31 { 32 printf("%d\n",sum); 33 } 34 else 35 { 36 ans = Roots(sum); 37 printf("%d\n",ans); 38 } 39 40 } 41 return 0; 42 }