728x90
반응형
6378번: 디지털 루트
양의 정수 N의 디지털 루트를 구하려면 N을 이루고 있는 모든 자리수를 더해야 한다. 이때, 더한 값이 한 자리 숫자라면, 그 수가 N의 디지털 루트가 된다. 두 자리 이상 숫자인 경우에는 다시 그
www.acmicpc.net
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
Main m = new Main();
m.doit();
}
public void doit() throws IOException{
// System.setIn(new FileInputStream("input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// int input = Integer.parseInt(br.readLine());
String input;
while(!(input = br.readLine()).equals("0")){
int ret = Integer.parseInt(input.substring(0, 1));
for(int i = 1; i < input.length(); i++){
ret += Integer.parseInt(input.substring(i, i+1));
if(ret >= 10){
ret = ret / 10 + ret % 10;
}
}
System.out.println(ret);
}
br.close();
}
}
728x90
반응형
'IT 정보 > 알고리즘(백준, BOJ)' 카테고리의 다른 글
[백준-BOJ] 8895 (0) | 2021.04.07 |
---|---|
[백준-BOJ] 7578 (0) | 2021.04.07 |
[백준-BOJ] 6359 (0) | 2021.04.07 |
[백준-BOJ] 5585 (0) | 2021.04.07 |
[백준-BOJ] 5565 (0) | 2021.03.02 |