1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
garik1379 [7]
3 years ago
7

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).
Computers and Technology
1 answer:
Oksana_A [137]3 years ago
5 0

Answer:

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;  

}  

You might be interested in
20 Points! What are some ways to insert a row or column? Check all that apply.
mars1129 [50]

Answer:

hi its me

Explanation:

5 0
4 years ago
Read 2 more answers
Discuss your views on multiple backgrounds. What are the advantages and disadvantages of having multiple backgrounds? Your submi
PSYCHO15rus [73]

Answer:

It is certainly possible to apply different backgrounds to elements. And these are being layered one over another. The first background is on the top and the last one at the back. And we can get a clue with a little designing knowledge that the last background can only have the background color.

And by the end of the process, we are up with the dynamic background design which is good enough to fit each of the web pages.

And it's quite simple to specify the multiple backgrounds. You can set multiple backgrounds to an element, and each of them. And these backgrounds are being layered atop over one and other, the first one being at the top. and the last one is listed at the back. However, as discussed, we can only have the last background to include the background color.

This can be done with the help of a shorthand background property, as well as various sub-properties of background like background-color. This is definitely possible.

There are some disadvantages, however, as well. The website loading speed can decrease, and hence SEO strategies can fail. Also, the visual effect that these multiple backgrounds creates can become boring after a certain period of time.

Explanation:

Please check the answer section.

7 0
3 years ago
What is a computer?
lyudmila [28]
All four are true, technically.

But the right answer is probably the third one.
3 0
4 years ago
Read 2 more answers
Design the program that reads two files and compares their contents. The program calls a method that reads the file one line at
Advocard [28]

Answer:

file1 = []

file2 = []

fileone = "lyric1.txt"

filetwo = "lyric2.txt"

def linereader( myfile, mylist):

   for line in open(myfile, "r") as file:

       line_read = readlines( line)

       mylist.append( line_read)

   file.close()

linereader( fileone, file1)

linereader( filetwo, file2)

# Assuming both files have same number of lines.

for index, item in enumerate(zip(file1, file2)):

   if item[0] != item[1]:

       print( index + 1, item)

Explanation:

The python code compares two files and returns the number and the lines from both files that do not match. The source code opens the files lyric1.txt and lyric2.txt reading each lines to different lists which are iterated to compare its items returning the number of the line and the unequal lines.

5 0
3 years ago
In chapter 3, we discussed syntax and semantics, in general there are two types of grammars for programming languages, regular a
WARRIOR [948]

Answer:

Lexical rules that are defined in case of regular grammar are simple and the notation is quite easy to understand.

Regular expression are useful for defining constructs of identifiers or constants. e.g. a|b etc.

In the case of context-free, grammar is not simple and deals with the productions.

Context-free are useful in describing the nested constructs like if-else etc which are not defined by regular expressions.

These produce a higher level of reliability as it provides a medium for generating syntactical as well as semantic data. The grammar is context-free is a little complex.

Explanation:

8 0
3 years ago
Other questions:
  • Is microsoft word the same as microsoft office?
    12·2 answers
  • A customer in a store is purchasing five items. Design a program that asks for the price of each item, and then displays the sub
    11·1 answer
  • What savings account has the best rate of return on his interest
    12·1 answer
  • Which of the following code correctly registers a handler with a button btOK?a. btOK.setOnAction(e -&gt; System.out.println("Han
    6·1 answer
  • Which type of business is best for Juanita to start? a corporation, because she needs a large investment to get started a sole p
    13·2 answers
  • The loop body instructions in the _____ statement always are processed at least once.
    6·1 answer
  • The question of ________ arises when considering the way in which online marketers gather consumers’ information over the Intern
    6·1 answer
  • Help help help help​
    6·1 answer
  • An analogue sensor has a bandwidth which extends from very low frequencies up to a maximum of 14.5 kHz. Using the Sampling Theor
    9·2 answers
  • Question 1 (1 point)
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!