Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would favor a recursive solution. You have some list of denominations, if the smallest one can evenly divide any remaining currency amount, this should work fine.</p> <p>Basically, you move from largest to smallest denominations.<br> Recursively,</p> <ol> <li>You have a current total to fill, and a largest denomination (with more than 1 left). If there is only 1 denomination left, there is only one way to fill the total. You can use 0 to k copies of your current denomination such that k * cur denomination &lt;= total.</li> <li>For 0 to k, call the function with the modified total and new largest denomination.</li> <li>Add up the results from 0 to k. That's how many ways you can fill your total from the current denomination on down. Return this number.</li> </ol> <p>Here's my python version of your stated problem, for 200 cents. I get 1463 ways. This version prints all the combinations and the final count total.</p> <pre><code>#!/usr/bin/python # find the number of ways to reach a total with the given number of combinations cents = 200 denominations = [25, 10, 5, 1] names = {25: "quarter(s)", 10: "dime(s)", 5 : "nickel(s)", 1 : "pennies"} def count_combs(left, i, comb, add): if add: comb.append(add) if left == 0 or (i+1) == len(denominations): if (i+1) == len(denominations) and left &gt; 0: comb.append( (left, denominations[i]) ) i += 1 while i &lt; len(denominations): comb.append( (0, denominations[i]) ) i += 1 print " ".join("%d %s" % (n,names[c]) for (n,c) in comb) return 1 cur = denominations[i] return sum(count_combs(left-x*cur, i+1, comb[:], (x,cur)) for x in range(0, int(left/cur)+1)) print count_combs(cents, 0, [], None) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload