Re: Change for a Dollar
- From: Mark P <usenet@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 24 Apr 2007 18:30:24 GMT
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;
-Mark
.
- Follow-Ups:
- Re: Change for a Dollar
- From: Charles Richmond
- Re: Change for a Dollar
- From: Fred Kleinschmidt
- Re: Change for a Dollar
- References:
- Change for a Dollar
- From: Charles Richmond
- Change for a Dollar
- Prev by Date: Change for a Dollar
- Next by Date: Re: Python has truly triumphed
- Previous by thread: Change for a Dollar
- Next by thread: Re: Change for a Dollar
- Index(es):
Relevant Pages
|