π» Coding Problems Solving/Dynamic Programming
[BOJ 2293] λμ 1
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]);
}
}