[백준] 9084 동전

장철현·2023년 11월 22일
0

백준

목록 보기
25/80

링크

9084 동전

문제

풀이

dp 문제... 어려웠다
오랜만에 풀어서 그런지 대충 규칙은 찾았지만 점화식 도출하는 것이 어려웠다.
결국 다른분들 블로그를 찾아보고 풀었다.
동전시리즈 더 풀어봐야겠다.

코드

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

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

        int testCase = Integer.parseInt(br.readLine());
        for(int t=0;t<testCase;t++){
            int n = Integer.parseInt(br.readLine());
            String[] arr = br.readLine().split(" ");
            int[] coins = new int[arr.length];

            for(int i=0;i<arr.length;i++){
                coins[i] = Integer.parseInt(arr[i]);
            }

            int m = Integer.parseInt(br.readLine());

            int[] dp = new int[m+1];
            dp[0] = 1;

            for(int i=0;i<coins.length;i++){
                int coin = coins[i];

                for(int j=coin;j<dp.length;j++){
                    dp[j] += dp[j-coin];
                }
            }

            System.out.println(dp[m]);

        }

    }


}

0개의 댓글

Powered by GraphCDN, the GraphQL CDN