題號:11332 簡單 Base CPE必考一星題 CPE歷屆試題

Online Judge - 11332 - Summing Digits

來源代碼:UVa 時間限制:3 s

題目說明

For a positive integer n, let f(n) denote the sum of the digits of n when represented in base 10. It is easy to see that the sequence of numbers $n, f(n), f(f(n)), f(f(f(n)))$, . . . eventually becomes a single digit number that repeats forever. Let this single digit be denoted g(n).

For example, consider $n = 1234567892$.

Then: $$f(n) = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 2 = 47$$ $$f(f(n)) = 4 + 7 = 11$$ $$f(f(f(n))) = 1 + 1 = 2$$ Therefore, $g(1234567892) = 2.$

輸入說明

Each line of input contains a single positive integer n at most 2,000,000,000. Input is terminated by $n = 0$ which should not be processed.

輸出說明

For each such integer, you are to output a single line containing $g(n)$.

輸入輸出範例

範例輸入
2
11
47
1234567892
0
範例輸出
2
2
2
2

討論

登入後即可加入討論。 登入

目前還沒有討論文章。

提交解答

登入後即可提交解答。

登入

最近提交

Yogurt
C/C++ · 2026-07-29 18:20
AC