-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry_coin.cpp
More file actions
48 lines (43 loc) · 875 Bytes
/
Copy pathtry_coin.cpp
File metadata and controls
48 lines (43 loc) · 875 Bytes
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
48
/* Coin changing problem using Dynamic Programming */
#include <iostream>
#define INF 999
using namespace std;
int n, A;
void coinChange(int d[], int S[], int C[]){
int min, coin;
for (int p=1; p<=A; p++){
min = INF;
for (int i=1; i<=n; i++){
if(d[i]<=p){
if(1+C[p-d[i]]<min){
min = 1+C[p-d[i]];
coin = i;
}
}
}
C[p]=min;
S[p]=coin;
}
}
void coinSet(int d[], int S[]){
cout<<"The coins used are: ";
while (A>0){
cout<<d[S[A]]<<" ";
A=A-d[S[A]];
}
}
int main(){
cout<<"Enter the value for which you want change: ";
cin>>A;
cout<<"Enter the number of coins available for change: ";
cin>>n;
int S[A+1], C[A+1], d[n+1];
cout<<"Enter the value of coins: ";
d[0]=0, C[0]=0, S[0]=0;
for (int i=1; i<=n; i++){
cin>>d[i];
}
coinChange(d, S, C);
coinSet(d, S);
return 0;
}