C++代码
  1. /*  
  2. Title: Digital Roots  
  3. Problem URL: http://acm.zju.edu.cn/show_problem.php?pid=1115  
  4. Author: Moqi  
  5. Date: 2007-12-30  
  6. Description:   
  7. */  
  8. #include <stdio.h>   
  9. #include <string.h>   
  10.   
  11. int main()   
  12. {   
  13.     char c;   
  14.     int n, sum;   
  15.   
  16.     while (1)   
  17.     {   
  18.         n = 0;   
  19.         while ((c = getchar()) != ‘\n’)   
  20.             n += (c – ‘0’);   
  21.         if (n == 0)   
  22.             break;   
  23.         do  
  24.         {   sum = 0;   
  25.             while(n != 0)   
  26.             {   
  27.                 sum += n % 10;   
  28.                 n /= 10;   
  29.             }   
  30.             n = sum;   
  31.         }while ((n / 10) >= 1);   
  32.         printf("%d\n", n);   
  33.         n = 0;   
  34.     }   
  35.     return 0;   
  36. }