-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum number of Coins.cpp
47 lines (42 loc) · 1.02 KB
/
Minimum number of Coins.cpp
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
39
40
41
42
43
44
45
46
47
#include<bits/stdc++.h>
using namespace std;
void minCoins(int);
int main() {
int t, n;
cin >> t;
while(t--) {
cin >> n;
minCoins(n);
cout << endl;
}
return 0;
}
void minCoins(int n) {
int d[10] = {1, 2, 5, 10, 20, 50, 100, 200, 500, 2000};
int arr[11][n + 1];
vector<int> vec;
for(int i = 1; i <= n; i++)
arr[0][i] = INT_MAX;
for(int i = 0; i <= 10; i++)
arr[i][0] = 0;
for(int i = 1; i <= 10; i++){
for(int j = 1; j <= n; j++){
if(j < d[i - 1]){
arr[i][j] = arr[i - 1][j];
}
else if(j - d[i - 1] >= 0)
arr[i][j] = min(arr[i - 1][j], 1 + arr[i][j - d[i - 1]]);
else
arr[i][j] = arr[i - 1][j];
}
}
int i = 10, j = n;
while(j > 0){
while(arr[i][j] == arr[i - 1][j])
i--;
vec.push_back(d[i - 1]);
j = j - d[i - 1];
}
for(int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
}