zju1115

Digital Roots

http://acm.zju.edu.cn/show_problem.php?pid=1115

   1 /*written by czk*/
   2 #include <stdio.h>
   3 #include <ctype.h>
   4 
   5 int main() {
   6     while(1) {
   7         int sum = 0;
   8         char c;
   9         while(!isdigit(c=getchar()));
  10         if(c=='0') break;
  11         while(isdigit(c)) {
  12             sum+= (c-'0');
  13             c = getchar();
  14         }
  15         while(sum >= 10) {
  16             int temp = sum;
  17             sum = 0;
  18             while(temp>0) {
  19                 sum += temp %10;
  20                 temp /= 10;
  21             }
  22         }
  23         printf("%d\n", sum);
  24     }
  25 }

zju1115 (2008-06-02 12:34:44由czk编辑)