To calculate the fewest number of coins to use for a given number of
cents, check if the highest value coin can be used. If it can, use one.
If the coin value is too big, check the next highest value coin, all
the way down to the lowest value coin until one can be used. After
using a coin, subtract its value from the remaining amount, and repeat
this process until the remaining amount is 0.
For example, you need to give 7 cents in change with quarters, dimes,
nickels, and pennies. To calculate the optimal change, check if a
quarter can be used. It can't, so check if a dime can be used. It also
can't, so check if a nickel can be used. It can, so use one, and
subract 5 from the remaining amount. Repeat this process until the
remaining amount is 0:
starting amount = 7
coin used = 5
remaining amount = 7 - 5 = 2
coin used = 1
remaining amount = 2 - 1 = 1
coin used = 1
remaining amount = 1 - 1 = 0
coins used = 5 1 1
For the exercises below, assume that the available coins are quarters,
dimes, nickels, and pennies. Write your answers like the example above:
5 1 1