Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

전공공부

[BOJ_15489] 파스칼의 삼각형 본문

Study/Problem Solving

[BOJ_15489] 파스칼의 삼각형

monitor 2024. 2. 25. 08:29

설명


기본 DP문제로 워밍업 용으로 좋은 문제 인 것 같습니다. 문제에서 주어진 파스칼의 삼각형 공식을 사용했으며 (오른쪽 위 왼쪽 위를 더하면 아래의 조합 경우의 수가 나옴) 이때, 문제에서 주어진 삼각형의 경우의 수만큼 더했습니다.

 

코드


package BOJ.Dynamic_Programming;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class BOJ_15489 {
    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String[] str = in.readLine().split(" ");

        int R = Integer.parseInt(str[0]) - 1;
        int C = Integer.parseInt(str[1]) - 1;
        int W = Integer.parseInt(str[2]);

        int[][] dp = new int[31][31];

        for (int i = 0; i < 31; i++){
            for (int j = 0; j <= i; j++){
                if(j == 0 || i == j){
                    dp[i][j] = 1;
                }
                if(dp[i][j] == 0) {
                    dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
                }
            }
        }

        int ans = 0;

        for (int i = R; i < R + W; i++){
            for (int j = C; j <= C + (i - R); j++){
                ans += dp[i][j];
            }
        }
        System.out.println(ans);
    }
}

 

 

15489번: 파스칼 삼각형

첫째 줄에 양의 정수 R, C, W가 공백을 한 칸씩 두고 차례로 주어진다. (단, 2 ≤ R+W ≤ 30, 2 ≤ C+W ≤ 30, 1 ≤ W ≤ 29, C ≤ R)

www.acmicpc.net

 

'Study > Problem Solving' 카테고리의 다른 글

[BOJ_15624] 피보나치 수 7  (0) 2024.03.02
[BOJ_10211] Maximum Subarray  (0) 2024.03.01
[BOJ_21317] 징검다리 건너기  (1) 2024.02.25
[BOJ_2156] 포도주 시식  (0) 2024.02.21
[BOJ_1890] 점프  (0) 2024.02.21