Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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_2293] 동전1 본문

Study/Problem Solving

[BOJ_2293] 동전1

monitor 2024. 3. 24. 12:27

설명


코인의 가치를 인덱스의 위치로 잡고 이에 따라서 인덱스의 위치 값에 따른 경우의 수를 구했습니다.

 

dp[i] += dp[i - 동전의 가치]

 

코드


package BOJ.Dynamic_Programming;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ_2293 {
    static int N,K;
    static int[] coins;
    static long[] dp;
    private static void input() throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine()," ");
        N = Integer.parseInt(tk.nextToken());
        K = Integer.parseInt(tk.nextToken());
        coins = new int[N];
        dp = new long[K + 1];
        for (int i = 0; i < N; i++){
            tk = new StringTokenizer(in.readLine()," ");
            coins[i] = Integer.parseInt(tk.nextToken());
        }
    }
    public static void main(String[] args) throws Exception{
        input();
        dp[0] = 1;
        for (int i = 0; i < N; i++){
            for (int j = coins[i]; j <= K; j++){
                if(j - coins[i] > K && j - coins[i] < 0){
                    continue;
                }
                dp[j] += dp[j - coins[i]];
            }
        }
        System.out.println(dp[K ]);
    }
}

 

 

2293번: 동전 1

첫째 줄에 n, k가 주어진다. (1 ≤ n ≤ 100, 1 ≤ k ≤ 10,000) 다음 n개의 줄에는 각각의 동전의 가치가 주어진다. 동전의 가치는 100,000보다 작거나 같은 자연수이다.

www.acmicpc.net

 

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

[BOJ_1758] 알바생 강호  (1) 2024.03.29
[BOJ_1343] 폴리오미노  (0) 2024.03.27
[BOJ_1106] 호텔  (0) 2024.03.23
[BOJ_22869] 징검다리 건너기 (small)  (0) 2024.03.17
[BOJ_1965] 상자 넣기  (0) 2024.03.14