Given a value V in cents, you need to make change using a minimum number of coin
ID: 3877215 • Letter: G
Question
Given a value V in cents, you need to make change using a minimum number of coins. Assume you have an infinite supply of quarters, nickels, dimes, and pennies. Find the minimum number of coins. Display the quantity of each coin and the total number of coins, similar to the example output below. Use global constant identifiers, of type unsigned int, for the values of quarters, nickels, dimes, and pennies. Example: const unsigned int QUARTER = 25: Do not use any other global variables. The data type for the value V must be unsigned integer. A company gives a bonus to all its employees. A 2% bonus on salary is given to employees who have been with theExplanation / Answer
Note that
quarter = 25 cents, dime = 10 cents, nickel = 5 cents, penny = 1 cent
C++ code:
#include<bits/stdc++.h>
using namespace std;
int CoinChange(vector<int> coins,int m, int change)
{
vector<int> S(change + 1,1000);
vector<int> seq(change + 1);
S[0] = 0;
for(int i = 1; i < change + 1; i++)
{
for(int j =0;j<m; j++)
{
if(i >= coins[j] && 1 + S[i-coins[j]] < S[i])
{
S[i] = 1 + S[i - coins[j]];
seq[i] = j;
}
}
}
map<string, int> mymap;
mymap["Quartar"] = 0; //quarter = 25 cents, dime = 10 cents, nickel = 5 cents, penny = 1 cent
mymap["dime"] = 0;
mymap["nickel"] = 0;
mymap["penny"] = 0;
int j = change;
while(j)
{
if(coins[seq[j]] == 1)
{
mymap["penny"] += 1;
}
else if(coins[seq[j]] == 5)
{
mymap["nickel"] += 1;
}
else if(coins[seq[j]] == 10)
{
mymap["dime"] += 1;
}
else
{
mymap["Quartar"] += 1;
}
j = j - coins[seq[j]];
}
cout << "Coins Needed: ";
for (std::map<string,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
{
std::cout << it->first << " => " << it->second << ' ';
}
cout<<endl;
// cout << "Total Coins Needed: " << S[change];
return S[change];
}
int main()
{
vector<int> coins;
coins.push_back(25);coins.push_back(10);
coins.push_back(5);coins.push_back(1);
int m = 4;
int V;
printf("Enter cents value ");
scanf("%d",&V);
printf("Minimum coins required is %d " , CoinChange(coins, m, V));
return 0;
}
Sample Output:
C:UsersAkashDesktopchegg>a
Enter cents value
99
Coins Needed:
Quartar => 3
dime => 2
nickel => 0
penny => 4
Minimum coins required is 9
C:UsersAkashDesktopchegg>a
Enter cents value
101
Coins Needed:
Quartar => 4
dime => 0
nickel => 0
penny => 1
Minimum coins required is 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.