• 문제 링크
4811번: 알약
입력은 최대 1000개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄이며, 병에 들어있는 약의 개수 N ≤ 30 가 주어진다. 입력의 마지막 줄에는 0이 하나 주어진다.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
long[][] cache = new long[31][31];
for (int i = 1; i < 31; i++) cache[0][i] = 1;
for (int i = 1; i < 31; i++) {
cache[i][0] = cache[i - 1][1];
for (int j = 1; j < 30; j++) cache[i][j] = cache[i - 1][j + 1] + cache[i][j - 1];
}
int n;
while ((n = read()) != 0) sb.append(cache[n][0]).append("\n");
bw.write(sb.toString());
bw.flush();
}
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' 카테고리의 다른 글
[백준] 10752 Cow Hopscotch - Dynamic Programming / Java (0) | 2024.01.19 |
---|---|
[백준] 11867 박스 나누기 게임 - Dynamic Programming / Java (0) | 2024.01.18 |
[백준] 25632 소수 부르기 게임 - Greedy / Java (0) | 2024.01.16 |
[백준] 31066 비 오는 날 - Greedy / Java (0) | 2024.01.15 |
[백준] 25287 순열 정렬 - Greedy / Java (0) | 2024.01.14 |
댓글