[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Back to Recursion, Revisited. On to Sorting Algorithms.
Held Wednesday, February 24
Summary
Contents
Handouts
Notes
(make-face-italic font-lock-comment-face) (make-face-bold font-lock-keyword-face) (make-face-bold font-lock-function-name-face) (make-face-bold font-lock-type-face)
/**
* Compute the minimum number of stamps to make up some postage.
* <br>Precondition: The postage is nonnegative.
* <br>Precondition: Stamp prices are positive.
* <br>Precondition: The stamp prices are sorted in increasing order.
* <br>Precondition: There is a penny stamp (so that we can make
* any price)
* <br>Postcondition: Computes the minimum number of stamps.
*/
public int minimumStamps(int postage, int[] stampPrices) {
/* Strategy: this algorithm uses a greedy technique. We
* repeatedly buy the largest possible stamp.
*/
// The number of stamps needed.
int count = 0;
// The index of the most expensive stamp to consider.
int stamp = stampPrices.length - 1;
// Repeatedly choose a price and remove copies until the
// postage reaches 0.
while (postage > 0) {
// Find the most expensive stamp we still need.
while (stampPrices[stamp] > postage) --stamp;
// Buy a stamp at the price.
postage -= stampPrices[stamp];
count +- 1;
} // while (postage > 0)
// That's it, we're done.
return count;
} // minimumStamps(int, int[])
for each stamp price, x compute the minumum number of stamps for postage-x add 1 choose the minimum of all those prices
/**
* Compute the minimum number of stamps required for some postage.
* All values are expressed in terms of cents.
* Pre: postage >= 0.
* Pre: stamp prices are positive.
* Pre: fewer than Integer.MAX_VALUE stamps are required.
* Post: returns the minimum number of stamps required for that
* postage (but does not print out which stamps).
* Post: throws an exception if it's not possible to make that
* postage with the given stamp prices.
*/
public int minStamps(int postage, int[] stampPrices)
throws Exception
{
// Our guess. Initialized to some outlandish value so that
// we can tell if we don't have a solution.
int guess = Integer.MAX_VALUE;
// The next guess. Used as we step through potential prices.
int nextGuess;
// Base case: no stamps are required for no postage.
if (postage == 0) return 0;
// Recursive case: try each price until we find the smallest.
else {
for(int i = 0; i < stampPrices.length; ++i) {
// If the current stamp is cheap enough, try to
// improve our guess.
if (stampPrices[i] <= postage) {
try {
// Make a new guess
nextGuess = 1 + minStamps(postage-stampPrices[i],
stampPrices);
if (nextGuess < guess) guess = nextGuess;
} // try
// No number of stamps could be used for revised postage.
// Don't update our guess.
catch (Exception e) { }
} // if the price was cheap enough.
} // for
// We've now computed the minimum number of stamps. Ensure
// that it's a reasonable value.
if (guess != Integer.MAX_VALUE) return guess;
// Otherwise, there was no way to sum stamp prices to the given
// postage, so throw an exception
else throw new Exception("No combination of stamps for " + postage);
} // Recursive case
} // minStamps(int,int[])
% ji StampComputer 7 2
% ji StampComputer 8 1 3 5
% ji StampComputer 8 5 3 1
% ji StampComputer 9 1 3 5
% ji StampComputer 27 10 9 1
MIN_STAMPS.
int[] MIN_STAMPS
MIN_STAMPS = new int[postage+1];
MIN_STAMPS[0] = 0;
for (int i = 1; i <= postage; ++i) {
MIN_STAMPS[i] = Integer.MAX_VALUE;
} // for
if (MIN_STAMPS[postage] != Integer.MAX_VALUE)
return MIN_STAMPS[postage];
MIN_STAMPS[postage] = guess;
NewStampComputer.java
% ji NewStampComputer 7 2
% ji NewStampComputer 8 1 3 5
% ji NewStampComputer 8 5 3 1
% ji NewStampComputer 9 1 3 5
% ji NewStampComputer 27 10 9 1
History
Back to Recursion, Revisited. On to Sorting Algorithms.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Examples] [Book] [Tutorial] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS152/99S/Outlines/outline.19.html
Source text last modified Fri Feb 26 08:45:06 1999.
This page generated on Fri Feb 26 09:48:43 1999 by SiteWeaver. Validate this page's HTML.
Contact our webmaster at rebelsky@math.grin.edu