https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1863 困難度 ★ 1.判斷是否為九的倍數=>每位數字加總在除以九,若整除即為九的被數,反之 2.若為九的倍數則要做9-degree ex:輸入 999999999999999999999 999999999999999999999 → 9+9+9+...+9 = 189 189 → 1+8+9 = 18 18 → 1+ = 9 9-degree=3
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
String str = scn.next(), temp = str;
if (str.equals("0"))
break;
int count = 1, tot;
while (true) {
tot = 0;
String arr[] = str.split("");
for (int i = 0; i < arr.length; i++)
tot += Integer.parseInt(arr[i]);
if (tot % 9 != 0 || tot / 10 == 0) {
break;
}
count++;
str = Integer.toString(tot);
}
if (tot % 9 != 0)
System.out.println(temp + " is not a multiple of 9.");
else
System.out.printf("%s is a multiple of 9 and has 9-degree %d.\n", temp, count);
}
}
/*
題目:Q10922: 2 the 9s
作者:1010
時間:西元 2016 年 7 月 */
}