最後更新於 2021 年 5 月 21 日
題目原文:https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=2307
題目概要:所有位數相加直至剩下個位數,並輸出該值。
假設n = 1234567892
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
因此,g(1234567892)= 2。
Input條件
每行輸入包含一個最多為2,000,000,000的正整數n。n = 0時結束程式。
Sample Input
2
11
47 (4+7=11 , 1+1 =2)
1234567892 (1+2+3+4+5+6+7+8+9+2=47 , 4+7=11 , 1+1 =2)
0
Sample Output
2
2
2
2
程式碼
算是比較簡單的一題,自行理解吧。
import java.util.*; public class main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ long n = sc.nextLong(); if(n==0) break; while(n/10!=0){ n = n/10 + n%10; } System.out.println(n); } } };
Latest posts by pluto (see all)
- 解決 preact-router 資源請求路徑錯誤的問題 - 2022 年 6 月 24 日
- [楓之谷私服] 潮流轉蛋機 NPC 腳本優化 - 2022 年 6 月 19 日
- [楓之谷私服] 簡單的飛天椅子(坐騎)改法 v120 - 2022 年 6 月 19 日