|
If you search the forum this is a common assignment and I have seen this topic go by before (think there may even be a snippet).
I would post some links but my internet is currently being a pain.
The basic principal is this. Say I have 57$ and 67 cents that I need to give in change. I start with the largest bill and I divide (integer division) 57/20 = 2 (and some remainder), then I use the %-mod (or remainder) operator to find the remainder 57 % 20 = 17... so I know I need 2 twenties, 17 dollers and 67 cents. Well now 17/10 = 1 and 17%10 = 7, you guest it, I need 2 twenties, 1 ten, and 7 dollers and 67 cents. 7/5 = 1 and 7%5 = 2 so now I am at 2 twenties, 1 ten, 1 five, 2 dollers, and 67 cents. 67/25=2, 67%2=17...etc etc.
Now there is another way (a little less fancy) to do this. The same way most pizza delivery divers do. 57-20=37-20=17 < 20 so I need 2 twenties. 17-10=7 < 10 so I need 1 ten. 7-5=2 < 5 so I need 1 five, and 2 dollars. belive it or not these two methods are just about the same.
|