• 문제 링크
1110번: 더하기 사이클
0보다 크거나 같고, 99보다 작거나 같은 정수가 주어질 때 다음과 같은 연산을 할 수 있다. 먼저 주어진 수가 10보다 작다면 앞에 0을 붙여 두 자리 수로 만들고, 각 자리의 숫자를 더한다. 그 다음,
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
Set<Integer> set = new HashSet<>();
int n = read(), cnt = 0;
do {
set.add(n);
cnt++;
} while (!set.contains(n = (n % 10) * 10 + (sum(n) % 10)));
bw.write(String.valueOf(cnt));
bw.flush();
}
private static int sum(int n) {
int sum = 0;
do sum += n % 10; while ((n /= 10) > 0);
return sum;
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 2675 문자열 반복 - Implementation / Java (0) | 2024.03.20 |
---|---|
[백준] 10809 알파벳 찾기 - Implementation / Java (0) | 2024.03.19 |
[백준] 1152 단어의 개수 - Implementation / Java (0) | 2024.03.17 |
[백준] 15008 Falling Apart - Greedy / Java (0) | 2024.03.16 |
[백준] 27951 옷걸이 - Greedy / Java (0) | 2024.03.15 |
댓글