forked from saif1448/AlgorithmLab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoinChange.cpp
More file actions
45 lines (38 loc) · 1.37 KB
/
Copy pathcoinChange.cpp
File metadata and controls
45 lines (38 loc) · 1.37 KB
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
#include <iostream>
using namespace std;
int coins[] = { 1,5,10,25,100 };
//This function finds the maximum number of coin by selecting the best choice at hand
int findMaxCoin(int amount, int size){
for(int i=0; i<size; i++){
if(amount < coins[i]) return i-1;
}
return -1;
}
//This the greedy approach implementation.
//This finds the selected coins to fulfill an amount
int GreedyApproachCoinChagne(int amount, int change[]){
int numOfCoins = sizeof(coins)/sizeof(coins[0]);
int count = 0;
while(//impelemnt the loop condition){
int k = findMaxCoin(amount, numOfCoins);
if(k == -1)
//implement your code
//implement what should it print if solution found
else{
//Implement your code to minus a coin from the total amount. If total amount is 60 and a coin chosen is 15 then it will be 60-15 = 45
// Implement your code to save this coin in the array name 'change'
}
}
return count;
}
int main(void) {
int change[10]; // This needs to be dynamic
int amount = 34;
int count = GreedyApproachCoinChagne(amount, change);
cout<< "\n Number of coins for change of " << amount<< " : " << count;
cout<< "\n Coins : ";
for(int i=0; i<count; i++)
{
cout<< change[i]<< " ";
}
}