Updated:

1. 문제 링크

https://www.acmicpc.net/problem/11047

2. 사용 알고리즘

그리디

3. 풀이

동전의 가치가 큰 동전을 최대한 많이 할당

4. 소스 코드

4-1. C++

https://github.com/dev-aiden/problem-solving/blob/main/boj/11047.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int arr[13];

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    int n, k; cin >> n >> k;
    for(int i = 0; i < n; ++i) cin >> arr[i];
    int ans = 0;
    for(int i = n - 1; i >= 0; --i) {
        ans += (k / arr[i]);
        k %= arr[i];
    }
    cout << ans << "\n";
    return 0;
}

4-2. JAVA

https://github.com/dev-aiden/problem-solving/blob/main/boj/11047.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

    static int[] arr = new int[13];

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());
        for(int i = 0; i < n; ++i) arr[i] = Integer.parseInt(br.readLine());
        int ans = 0;
        for(int i = n - 1; i >= 0; --i) {
            ans += (k / arr[i]);
            k %= arr[i];
        }
        System.out.println(ans);
        br.close();
    }
}

Updated:

Leave a comment