Algorithm to solve minimal number of coins problem.
The problem is: What is the minimal number of coins needed to make a change?
For given coin nominals
500, 200, 100, 50, 20, 10, 5, 2, 1, 0.50, 0.25, 0.1, 0.05, 0.01
What coins (or banknotes) will be given as a change for 254.78?
To solve this we need to check how many coins or banknotes of each nominal beginning from highest to lowest can be “fitted” for each part of the value.
The answer is:
200.0 50.0 2.0 2.0 0.5 0.25 0.01 0.01 0.01
1. Store initial money amount in variable M. 2. Place money nominals from highest to lowest in an array A. Create index to this array and name it I - it should be zero in the beginning. 3. Check if M < A[I]. If it is true increase I by one. If not subtract A[I] from M. 4. Check if M >= 0.01 <- or if it is bigger than smallest coin in array A <- last element of that array. If so go to step 3.