Created
August 25, 2016 14:49
-
-
Save developer-sdk/b4192a0ee0981a8bdbaffa4d46156692 to your computer and use it in GitHub Desktop.
정올, 그리디알고리즘, 동전자판기
This file contains hidden or 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.algo.greedy; | |
| import java.util.Scanner; | |
| /** | |
| * 동전자판기 하 | |
| * | |
| * 가지고 있는 동전으로 구할 수 있는 최대 개수의 동전을 구하고 | |
| * 남은 동전이 최소의 동전 개수이다. | |
| * | |
| * http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=466&sca=3020 | |
| * http://m.blog.naver.com/skyblue_2002/220631970001 | |
| * | |
| * @author whitebeard | |
| * | |
| */ | |
| public class Problem1183 { | |
| public static void main(String[] args) { | |
| Scanner in = new Scanner(System.in); | |
| int n = in.nextInt(); | |
| int[] coins = new int[6]; | |
| for(int i = 0; i < 6; i++) | |
| coins[i] = in.nextInt(); | |
| in.close(); | |
| // int n = 212; | |
| // int[] coins = { 4, 2, 0, 4, 3, 4 }; | |
| int[] values = { 500, 100, 50, 10, 5, 1 }; | |
| int max = 0; | |
| for(int i = 0; i < 6; i++ ) { | |
| max += values[i] * coins[i]; | |
| } | |
| max -= n; | |
| int index = 0; | |
| while(true) { | |
| if(max <= 0) { | |
| break; | |
| } | |
| if(coins[index] == 0 || max < values[index]) { | |
| index++; | |
| continue; | |
| } | |
| max -= values[index]; | |
| coins[index]--; | |
| } | |
| int sum = 0; | |
| for(int coin : coins) | |
| sum += coin; | |
| System.out.println(sum); | |
| for(int coin : coins) | |
| System.out.printf("%d ", coin); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment