C++代码
- /*
- Title: Digital Roots
- Problem URL: http://acm.zju.edu.cn/show_problem.php?pid=1115
- Author: Moqi
- Date: 2007-12-30
- Description:
- */
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char c;
- int n, sum;
- while (1)
- {
- n = 0;
- while ((c = getchar()) != ‘\n’)
- n += (c – ‘0’);
- if (n == 0)
- break;
- do
- { sum = 0;
- while(n != 0)
- {
- sum += n % 10;
- n /= 10;
- }
- n = sum;
- }while ((n / 10) >= 1);
- printf("%d\n", n);
- n = 0;
- }
- return 0;
- }