My assignment is to write an algorithm using brute force to determine the number of distinct ways, an related combinations of change for given amount. The change will be produced using the following coins: penny (1 cent), nickel (5 cents), dime (10 cents), and quarter (25 cents).
e.g.
Input: 16 (it means a change of 16 cents)
Output: can be produced in 6 different ways and they are:
- 16 pennies.
- 11 pennies, 1 nickel
- 6 pennies, 1 dime
- 6 pennies, 2 nickels
- 1 penny, 3 nickels
- 1 penny, 1 nickel, 1 dime
My algorithm must produce all possible change combinations for a specified amount of change.
I am at a complete loss as to how to even begin starting an algorithm like this. Any input or insight to get me going would be awesome.
Ok. Let me explain one idea for a brute force algorithm. I will use recursion here.
Let’s you need a change of
ccents. Then considercasor simply,
Now you need to go through all the possible values for
p,n,dandqthat equals the value ofc. Using recursion, for eachp in [0, maximumPennies]go through eachn in [0, maximumNickels]. For eachngo through eachd in [0, maximumDimes]. For eachdgo through eachq in [0, maximumQuarters].For any equality in these steps you got a solution.