Re: Change for a Dollar
- From: Charles Richmond <frizzle@xxxxxxxxx>
- Date: Tue, 24 Apr 2007 21:02:04 -0500
Mark P wrote:
Charles Richmond wrote:I heard one night on David Letterman that there are
293 ways to make change for a dollar. So I started
thinking about writing a program to list out *all*
the ways a dollar could be broken into change. I
just can *not* seem to get a hold on this problem.
I wrote a program with a recursive function attempting
to make the change, but it had excessive runtime and
does *not* seem to be working in any case...
Does anyone have an idea how I can proceed???
There are smarter ways to do it but a brain dead simple approach that ought to be fast enough on any machine built in the last twenty years...
int n_ways = 0;
for( n_50 = 0; n_50 <= 2; ++n_50)
for( n_25 = 0; n_25 <= 4; ++n_25)
for( n_10 = 0; n_10 <= 10; ++n_10)
for( n_5 = 0; n_5 <= 20; ++n_5)
if( n_50 * 50 + n_25 * 25 + n_10 * 10 + n_5 *5 <= 100)
++n_ways;
This may *count* the number of ways change can be made. I am
*not* sure that these loops do what you (or I) think they do.
But for my program, I want to *list* all the ways of making
change. So lines of the list might be:
1) 100 pennies, 0 nickels, 0 dimes, 0 quarters, and 0 halves
......
58) 55 pennies, 2 nickels, 1 dimes, 1 quarter, and 0 halves
For the sake of this program (and David Letterman ;-), we can
only use pennies, nickels, dimes, quarters, and halves. David
is *not* smart enough to know about 2 and 3 cent pieces. ;-)
--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
.
- Follow-Ups:
- Re: Change for a Dollar
- From: Anton Treuenfels
- Re: Change for a Dollar
- From: Mark P
- Re: Change for a Dollar
- References:
- Change for a Dollar
- From: Charles Richmond
- Re: Change for a Dollar
- From: Mark P
- Change for a Dollar
- Prev by Date: Re: Change for a Dollar
- Next by Date: Re: Change for a Dollar
- Previous by thread: Re: Change for a Dollar
- Next by thread: Re: Change for a Dollar
- Index(es):
Relevant Pages
|