Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

22.03.14 - [BOJ] 11047. 동전 0 #226

Closed
suhyunsim opened this issue Mar 14, 2022 · 0 comments
Closed

22.03.14 - [BOJ] 11047. 동전 0 #226

suhyunsim opened this issue Mar 14, 2022 · 0 comments
Assignees
Labels
그리디 그리디 알고리즘 성공 맞은 문제 실버 BOJ - 실버

Comments

@suhyunsim
Copy link
Owner

suhyunsim commented Mar 14, 2022

문제

핵심 아이디어

증명: 동전 개수를 최소화 하려면 500원 동전을 최대한 많이 써야 한다?

  • 보조 정리 1: 동전을 최소로 소모하면서 물건값을 지불하려면 10/100원 동전은 4개 이하, 50원 동전은 1개 이하로 사용 (by 귀류법: 명제가 거짓이라면 모순이 발생함을 보이는 증명)
    • 거짓이라면?
    • 10/100원 동전을 5개 이상 사용 -> 50/500원 동전으로 대체
    • 50원 동전을 2개 이상 사용 -> 100원 동전으로 대체
  • 보조 정리 1 귀류법으로 증명을 할 수 있음
  • 500 -> 100 -> 50 -> 10... 순서로
  • 배수 관계가 성립해서 가능, 성립하지 않는다면? 같은 풀이가 맞을거라는 보장없음

어려운 점, 실수

풀이

package main.java.com.poogle.BOJ.Q11047;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] cost = new int[n];
        for (int i = 0; i < n; i++) {
            cost[i] = sc.nextInt();
        }
        int answer = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (cost[i] <= k) {
                int cnt = k / cost[i];
                answer += cnt;
                k -= cost[i] * cnt;
            }
        }
        System.out.println(answer);
    }
}
@suhyunsim suhyunsim added 실버 BOJ - 실버 그리디 그리디 알고리즘 labels Mar 14, 2022
@suhyunsim suhyunsim self-assigned this Mar 14, 2022
@suhyunsim suhyunsim added the 성공 맞은 문제 label Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
그리디 그리디 알고리즘 성공 맞은 문제 실버 BOJ - 실버
Projects
None yet
Development

No branches or pull requests

1 participant