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
52 lines (44 loc) · 984 Bytes
/
Copy pathcoinChange.cpp
File metadata and controls
52 lines (44 loc) · 984 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
49
50
51
52
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
int coins[]={1,5,10,25,100};
int findMaxCoin(int amount,int size)
{
for(int i=0;i<size;i++)
{
if(amount<coins[i])
return i-1;
}
return -1;
}
int GreedyApproachCoinChagne(int amount,int change[])
{
int numOfCoins = sizeof(coins)/sizeof(coins[0]);
int count = 0;
while(amount)
{
int k=findMaxCoin(amount,numOfCoins);
if(k==-1)
{
cout<<"No";
}
else
{
amount=amount-coins[k];
change[count++]=coins[k];
}
}
return count;
}
int main(void)
{
int change[10];
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]<< " ";
}
}