[BOJ][브2][5585] 거스름돈

문제 링크

문제링크

첫 번째 풀이 : Greedy

알고리즘

입력받는 n은 물건 값이고, 거스름돈은 1000 - n원입니다.

가장 큰 단위의 동전부터 최대한 가져가면 됩니다.

정답코드

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
37
38
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using pii = pair<int, int>;

const int MAX = 5010;
const int MOD = 10007;
int arr[MAX][MAX];
ll dp[MAX][2];
bool visited[MAX];

int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };

int n, m;

void show(void);
void bfs(int x, int y);

int coins[] = { 500,100,50,10,5,1 };

int main()
{
    //freopen("input.txt", "r", stdin);
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n;
    n = 1000 - n;
    int ans = 0;
    for (int i = 0; i < 6; i++) {
        if (n >= coins[i]) {
            ans += n / coins[i];
            n -= (n / coins[i]) * coins[i];
        }
    }
    cout << ans;
    return 0;
}

Success Notice: 수고하셨습니다. :+1:

Leave a comment