티스토리 뷰
N개의 동전의 가치를 합하여, K원을 만드는 경우를 구한다.
1, 2, 5 원의 동전을 이용하여 10원을 만드는 경우는 다음과 같다.
사용동전 | 1 |
2 |
5 | 개수 |
1 | 1 |
|
| 1 |
2 | 1+1 |
2 |
| 2 |
3 | 1+1+1 |
1+2 |
| 2 |
4 | 1+1+1+1 |
1+1+2, 2+2 |
| 3 |
5 | 1+1+1+1+1 |
1+1+1+2, 1+2+2 |
5 | 4 |
5원을 만드는 경우는 위와 같다.
1원동전 사용: 1원을 5개 사용
2원동전 사용: 3원을 만드는 경우에 2원을 더하여 생성
5원동전 사용: 0원을 만드는 경우에 5원을 더하여 생성
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sdk.backjun.dp; | |
import java.util.Scanner; | |
/** | |
* 백준, 동전 1, 2293 | |
* | |
* @author whitebeard-k | |
* | |
*/ | |
public class Problem2293 { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int N = sc.nextInt(); | |
int K = sc.nextInt(); | |
int[] coins = new int[N+1]; | |
for(int i = 1; i <= N; i++) | |
coins[i] = sc.nextInt(); | |
sc.close(); | |
int[] dp = new int[K+1]; | |
dp[0] = 1; | |
for(int i = 1; i <= N; i++ ){ | |
for(int j = 0; j <= K; j++ ){ | |
if(j - coins[i] >= 0) { | |
dp[j] += dp[j-coins[i]]; | |
} | |
} | |
System.out.printf("coin %d: ", coins[i]); | |
for(int n : dp) | |
System.out.printf("%d ", n); | |
System.out.println(); | |
} | |
System.out.println(dp[K]); | |
} | |
} |
반응형
'알고리즘 > 정올' 카테고리의 다른 글
[정올][다이나믹] 1491 자동차경주대회 (0) | 2018.01.25 |
---|---|
[정올][다이나믹] 1019 소형기관차 (0) | 2017.07.07 |
[정올][bfs] 2613 토마토(고) (0) | 2017.05.12 |
[정올][다이나믹] 1411 두 줄로 타일 깔기 (0) | 2017.04.25 |
[정올][다이나믹] 1408 전깃줄(초) (0) | 2017.04.24 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 정올
- hbase
- HDFS
- 알고리즘
- Hadoop
- build
- emr
- 파이썬
- Python
- AWS
- Tez
- S3
- yarn
- oozie
- SPARK
- nodejs
- java
- mysql
- airflow
- bash
- 오류
- HIVE
- 백준
- ubuntu
- Linux
- error
- 다이나믹
- 하둡
- SQL
- 하이브
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함