problem description
the digital
root of a positive integer is found by summing the digits of the
integer. if the resulting value is a single digit then that digit is the
digital root. if the resulting value contains two or more digits, those
digits are summed and the process is repeated. this is continued as
long as necessary to obtain a single digit.
for example, consider the
positive integer 24. adding the 2 and the 4 yields a value of 6. since 6
is a single digit, 6 is the digital root of 24. now consider the
positive integer 39. adding the 3 and the 9 yields 12. since 12 is not a
single digit, the process must be repeated. adding the 1 and the 2
yeilds 3, a single digit and also the digital root of 39.
input
the
input file will contain a list of positive integers, one per line. the
end of the input will be indicated by an integer value of zero.
output
for each integer in the input, output its digital root on a separate line of the output.
sample input
24
39
sample output
6
3
題目簡單~:
1
2
4
5
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
25
26
27
28
29
30
31
32
33
34
<code>#include <iostream></code>
<code>using</code> <code>namespace</code> <code>std;</code>
<code>int</code> <code>main(</code><code>int</code> <code>argc,</code><code>char</code> <code>*argv[])</code>
<code>{</code>
<code> </code><code>char</code> <code>c;</code>
<code> </code><code>int</code> <code>sum = 0;</code>
<code> </code>
<code> </code><code>while</code><code>(c =</code><code>getchar</code><code>())</code>
<code> </code><code>{</code>
<code> </code><code>if</code><code>(c ==</code><code>'\n'</code><code>)</code>
<code> </code><code>{</code>
<code> </code><code>cout << sum << endl;</code>
<code> </code>
<code> </code><code>c =</code><code>getchar</code><code>();</code>
<code> </code><code>if</code><code>(c ==</code><code>'0'</code><code>)</code>
<code> </code><code>break</code><code>;</code>
<code> </code><code>else</code>
<code> </code><code>sum = c -</code><code>'0'</code><code>;</code>
<code> </code>
<code> </code><code>continue</code><code>;</code>
<code> </code><code>} </code>
<code> </code>
<code> </code><code>sum += c -</code><code>'0'</code><code>;</code>
<code> </code><code>if</code><code>(sum > 9)</code>
<code> </code><code>sum = sum%10 + sum/10;</code>
<code> </code><code>}</code>
<code> </code>
<code> </code><code>}</code>
<code> </code><code>return</code> <code>0;</code>
<code>}</code>