백준 2775번 부녀회장이 될테야(java)

마뇽미뇽·2024년 7월 8일
0

알고리즘 문제풀이

목록 보기
74/165

1.문제

https://www.acmicpc.net/problem/2775

2.풀이

https://st-lab.tistory.com/78님을 참고하였다. 코드를 이해하기 위해 주석을 달면서 다시 풀었다.

3.코드

package com.example.baekjoon;

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int t = Integer.parseInt(br.readLine());    //  testcase
        int apt[][] = new int[15][15];  // 14까지니까

        for (int i = 0; i < 15; i++) {  //  기본값 설정
            apt[i][1] = 1;  // i층 1호
            apt[0][i] = i;  // 0층 i호
        }

        for (int i = 1; i < 15; i++) {  //  14층 까지
            for (int j = 2; j < 15; j++) {  //  호수, 1호는 이미 기본값으로 설정되어 있음
                apt[i][j] = apt[i - 1][j] + apt[i][j - 1];   //  한 층 빼고 그 밑에 호수 사람들 더한 값이 해당 층의 사람들
            }
        }

        for (int i = 0; i < t; i++) {
            int k = Integer.parseInt(br.readLine());
            int n = Integer.parseInt(br.readLine());
            System.out.println(apt[k][n]);
        }
    }
}
profile
Que sera, sera

0개의 댓글