![ashleygarcia0884](/avatars/13567.jpg)
ashleygarcia0884
25.10.2019 •
Computers and Technology
Consider the problem of making change for n cents using the fewest number of coins. assume that each coins value is an integer. a. you have available coin denominations of 1 cent, 5 cents, 10 cents, 25 cents, as in the us coins of penny, nickel, dime, quarter. assume there is infinite availability of each of these four coin denomination types. describe a greedy algorithm to make change using the fewest number of coins. b. consider your greedy algorithm and making change for n=30 cents. show that the optimal solution for making change includes your greedy choice. to do so, assume that your greedy choice is not in the optimal solution. then use the "cut and paste" argument to show that you can replace other coins by your greedy choice, therefore finding a better solution than what was assumed to be optimal. this is in contradiction to the assumption, so your greedy choice is in the optimal solution. c. although the greedy algorithm is optimal for the us coins, it is not necessarily optimal for any set of coins. give an example of a different set of made up coin denominations with different values, for which the greedy algorithm does not yield an optimal solution. your set should include a penny so that there is a solution for every value of n. as before, assume there is infinite availability of each of the coin denomination types. show specifically a counter example in which the greedy algorithm yields a change solution that is not optimal, that is, does not include the fewest number of coins. d. name another algorithmic approach that will find an optimal solution to your example in (c).
Solved
Show answers
More tips
- H Health and Medicine What to Eat to Lose Weight?...
- S Society and Politics Will Japan become Russia s Military Enemy?...
- P Philosophy Is Everything We Strive for Eventually Achieved and Destroyed?...
- S Society and Politics Understanding Politics and Its Role in the Development of Civilization...
- P Philosophy Why Did God Create Man and Place Him in Obscurity?...
- S Society and Politics Skoptsy: Who They Are and How They Perform Castration?...
- O Other Childhood Fears: What Many of Us Experienced...
- P Philosophy What is Something for you?...
- H Health and Medicine Why Do Humans Have One Heart?...
- P Philosophy Unbelievable stories of encounters with otherworldly forces...
Answers on questions: Computers and Technology
- C Computers and Technology anyone who can pull an ip/DDoS(preferably both) plz answer i am trying to find the main account behind thebitly bots and i need some1 to DDoS them...
- C Computers and Technology How to trace the dynamic behavior of programs that include conditional execution, loops, and user-defined functions...
- C Computers and Technology Imagine that a message M is encrypted with Alice s private key K_private. C = Enc(K_private, M) The resulting ciphertext C is sent over the network to Bob who decrypts this message...
- C Computers and Technology Usefulness of information is a function of: A. artificial intelligence. B. software agency. C. networking. D. quality, timeliness, and completeness. E. time....
- C Computers and Technology are small computers programs designed to perform automated, repetitive task of collecting and archiving webpagess over the internet....
- H History 1. What are your thoughts on the Nacirema culture?...
- M Mathematics 2. In each of the following compound interest equations with t representing the account term, determine the number of times the account is compounded per year and the interest rate...
- P Physics Identify each statement is an example of melting or sublimination...
- M Mathematics Find the unknown length y in the following pair of similar triangles....
- H History Need help ASAP please...
Ответ:
There are two algorithms in which apply different optimal solutions.
They are: A Dynamic and Naive recursive programs
Explanation:
// A Naive recursive C++ program to find minimum of coins
// to make a given change V
#include<bits/stdc++.h>
using namespace std;
// m is size of coins array (number of different coins)
int minCoins(int coins[], int m, int V)
{
// base case
if (V == 0) return 0;
// Initialize result
int res = INT_MAX;
// Try every coin that has smaller value than V
for (int i=0; i<m; i++)
{
if (coins[i] <= V)
{
int sub_res = minCoins(coins, m, V-coins[i]);
// Check for INT_MAX to avoid overflow and see if
// result can minimized
if (sub_res != INT_MAX && sub_res + 1 < res)
res = sub_res + 1;
}
}
return res;
}
// Driver program to test above function
int main()
{
int coins[] = {9, 6, 5, 1};
int m = sizeof(coins)/sizeof(coins[0]);
int V = 11;
cout << "Minimum coins required is "
<< minCoins(coins, m, V);
return 0;
}
// A Dynamic Programming based C++ program to find minimum of coins
// to make a given change V
#include<bits/stdc++.h>
using namespace std;
// m is size of coins array (number of different coins)
int minCoins(int coins[], int m, int V)
{
// table[i] will be storing the minimum number of coins
// required for i value. So table[V] will have result
int table[V+1];
// Base case (If given value V is 0)
table[0] = 0;
// Initialize all table values as Infinite
for (int i=1; i<=V; i++)
table[i] = INT_MAX;
// Compute minimum coins required for all
// values from 1 to V
for (int i=1; i<=V; i++)
{
// Go through all coins smaller than i
for (int j=0; j<m; j++)
if (coins[j] <= i)
{
int sub_res = table[i-coins[j]];
if (sub_res != INT_MAX && sub_res + 1 < table[i])
table[i] = sub_res + 1;
}
}
return table[V];
}
// Driver program to test above function
int main()
{
int coins[] = {9, 6, 5, 1};
int m = sizeof(coins)/sizeof(coins[0]);
int V = 11;
cout << "Minimum coins required is "
<< minCoins(coins, m, V);
return 0;
}
Ответ: