Kim_dev 2022. 6. 29. 04:34

[BOJ 2293] λ™μ „1

 

1. 문제 : Link

동전 μ’…λ₯˜ n

λ§Œλ“€μ–΄μ•Όν•˜λŠ” κ°’ k

λ₯Ό λ°›μ•„μ„œ kλ₯Ό λ§Œμ‘±μ‹œν‚€λŠ” λ™μ „μ˜ ν•© 경우의 수λ₯Ό κ΅¬ν•˜λŠ” 문제

 

2. 풀이

동적 κ³„νšλ²•μ„ ν™œμš©ν•˜λŠ” 문제

배열에 κ³„μ‚°λœ 값을 λ„£μ–΄μ£ΌλŠ” 것을 λ°˜λ³΅ν•œλ‹€.

 

μ‰½κ²Œ λ§ν•˜λ©΄,

1,2,5둜 7μ΄λΌλŠ” 숫자λ₯Ό λ§Œλ“œλŠ” 경우의 μˆ˜λŠ” 7이 λ˜κΈ°μœ„ν•œ λͺ¨λ“  경우의 μˆ˜λ“€μ΄ 더해진 결과이기 λ•Œλ¬Έμ— 동적 κ³„νšλ²•μ΄ μ‚¬μš©λœ 것이라 ν•  수 μžˆλ‹€.

https://pacific-ocean.tistory.com/200

 

3. μ½”λ“œ

package baekjoon;

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

public class Main {
    static int N;
    static int sum;
    static int[] coins;
    static int[] dp;

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        N = sc.nextInt();
        sum = sc.nextInt();

        coins = new int[N];

        for(int i=0; i<N; i++){
            coins[i] = sc.nextInt();
        }

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

        for(int i = 0; i < N; i++) {
            for(int j = coins[i]; j <= sum; j++) {
                dp[j] += dp[j - coins[i]];
            }
        }

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

    }
}