반응형
문제
https://www.acmicpc.net/problem/2775
코드
import java.io.*;
public class Main {
private static int[][] apart;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
StringBuilder sb = new StringBuilder();
apart = new int[15][15];
makeApart();
for (int i = 0; i < T; i++) {
int k = Integer.parseInt(br.readLine());
int n = Integer.parseInt(br.readLine());
sb.append(apart[k][n]).append("\n");
}
System.out.println(sb);
br.close();
}
private static void makeApart() {
for (int i = 0; i < 15; i++) {
for (int j = 1; j < 15; j++) {
if (i == 0){
apart[0][j] = j;
continue;
}
apart[i][j] = apart[i][j - 1] + apart[i - 1][j];
}
}
}
}
풀이
0층
if (i == 0){
apart[0][j] = j;
continue;
}
- 0층의 i호에는 i명이 산다는 조건이 존재하므로 따로 처리해준다.
그외의 층
apart[i][j] = apart[i][j - 1] + apart[i - 1][j];
- a층의 b호 → a-1층의 1호부터 b호까지 사람들의 수의 합 만큼의 사람
- 현재 층(`i`)의` j`호에 거주하는 사람 수 = 같은 층의 바로 왼쪽 호(`j-1`)에 있는 사람 수 + 바로 아래 층(`i-1`)의 같은 호(`j`)에 있는 사람 수
층(i) \ 호(j) | 1 | 2 | 3 | 4 | 5 |
0층 | 1 | 2 | 3 | 4 | 5 |
1층 | 1 | 3 | 6 | 10 | 15 |
2층 | 1 | 4 | 10 | 20 | 35 |
반응형
'백준' 카테고리의 다른 글
[백준] 7568: 덩치 (0) | 2025.04.18 |
---|---|
[백준] 4949: 균형잡힌 세상 (0) | 2025.04.18 |
[백준] 21736: 헌내기는 친구가 필요해 (0) | 2025.03.27 |
[백준] 7662: 이중 우선순위 큐 (0) | 2025.03.25 |
[백준] 7662: 이중 우선순위 큐 (0) | 2025.03.22 |