[백준 2145번] 숫자 놀이
[백준 2145번] 숫자 놀이
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class 백준_손수경_정답_2145 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (true) { String num = br.readLine(); if (num.equals("0")) { break; } String[] splitNum = num.split(""); System.out.println(sum(splitNum)); } } public static int sum(String[] nums) { int sum = 0; for (int i = 0; i < nums.length; i++) { sum += Integer.valueOf(nums[i]); } if (sum / 10 != 0) { return sum(String.valueOf(sum).split("")); } else { return sum; } } } | cs |
댓글남기기