BOJ::2293 동전 1
https://www.acmicpc.net/problem/2293
동전을 입력 받는게 아닌 1,2,5원이 있다고 가정해보자.
d[n] = d[n-1]+d[n-2]+d[n-5] 이다.
이를 이용하면 풀 수있다.
<JAVA>
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 32 33 34 35 36 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static int n,k; static int a[]=new int[101]; static int d[]=new int[10001]; public static void main(String args[]) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); n=Integer.parseInt(st.nextToken()); k=Integer.parseInt(st.nextToken()); for(int i = 1 ; i <= n ; i++){ a[i]=Integer.parseInt(br.readLine()); } for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= k ; j++){ if(a[i]==j){ d[j]++; }else if(a[i]<=j){ d[j]+=d[j-a[i]]; } } } System.out.println(d[k]); } } | cs |
<C++>
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 | #include <iostream> using namespace std; int n, k; int a[101]; int d[10001]; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int j = 1; j <= n; j++) { for (int i = 1; i <= k; i++) { if (i == a[j]) { d[i]++; } else if (a[j] <= i) { d[i] += d[i - a[j]]; } } } cout << d[k] << endl; } | cs |
'BOJ::문제풀이' 카테고리의 다른 글
2563 색종이 (0) | 2018.01.01 |
---|---|
2309 일곱 난쟁이 (0) | 2018.01.01 |
2217 로프 (0) | 2018.01.01 |
2206 벽 부수고 이동하기 (2) | 2018.01.01 |
2193 이친수 (0) | 2018.01.01 |