Sie sind auf Seite 1von 3

11/13/11

Algorithm for bank notes : algorithm


Hello a11096277 M Account Upgrade Account Logout

Enter Your Search Terms

Experts Exchange

Algorithm for bank notes

Vie ing a Solution


05/24/00 11:34 PM, ID: 10356620

My Profile: Edit View Know ledgebase Skin: Premium Expert Open Questions (0) Question History (0) Status: Limited Access

Algorithm for bank notes


Asked by martyn_bannister in Miscellaneous Programming Tags: algorithm I need a way to solve the following problem:

6.8
You have earned 0 points!

We have orders coming in using, say, US Dollars. The orders are always for a whole number of dollars (i.e. no cents!). We want to satisfy each order with a number of bank notes of particular known denominations. I need a formula or algorithm that a) Lets me know if it is possible to satisfy the order (for example we couldn't satisfy an order for 991 dollars if we only have 100, 50, 10 and 5 dollar notes) b) Gives me the least number of notes and their denominations to satisfy the order if it IS possible (for example, an 11 dollar order could be satisfied with 11 one dollar notes, but it would be better to satisfy it with 2 * 5 dollar and 1 * 1 dollar notes, if they are available). Can any of you maths aware people out there point me at a way of solving the above two points?
05/24/00 11:38 PM, ID: 2843752

Earn 10,000 m ore points and get FREE Unlimited Expert Access. Learn More

1. Com plete our E pert profile Share your qualifications and areas of expertise w ith fellow experts and users. Go to your profile

m art n_bannister: Alright, I just spotted it!! We could satisfy an order for 11 dollars from 5 and 2 dollar notes! (1 * 5 + 3 * 2).

Was this comment helpful? 2. Set our Question Alerts Use Expert Filters to receive notifications w henever a new question is asked in your area of expertise. Set up your filters

How about using an example of 991 dollars w ith only 100, 50 and 10 dollar notes available? Sorry!

3. Start Ans ering Questions 05/25/00 02:26 AM, ID: 2844241 Find questions and start posting! Askers w ill choose the best solution from the expert comments. If your solution is selected you'll earn points tow ard free membership. View questions

Lischke:

Was this comment helpful?

I think this is not hard to do. Just go into a loop and subtract the largest bank note from the amount until the rest is smaller than this bank note value. The go to the next smaller (and available !) note until you find one w hich is smaller than the rest amount. Subtract again and count how many times until the rest amount is zero or smaller than the current bank note. If you cannot solve this loop w ith a rest of 0 (i.e. you have no smaller bank note to subtract) then point 1) is solved and you can say it is not possible to satisfy the order. If you can make it to 0 then point 2) is solved too as you have counted the number of notes needed to get to 0. Once you have this loop w orking you can change it to be more elegant by using modulo division. Ciao, Mike

4. Write a Technolog Article If you have extensive know ledge in any specific field, you can earn points by w riting an article. Original tips, tricks and tutorials are all accepted. Write an article

05/25/00 02:31 AM, ID: 2844254

Lischke:

Was this comment helpful?

Oops, should have been integer division not modulo. For your example of 991 dollars and only 100, 50 and 10 dollar nodes available: 1) 991 div 100 (highest dollar note) = 9 -> you need 9 100 dollar notes, rest is 91 2) 91 div 50 (next smaller bank note) is 1 -> you need one 50 dollar note, rest is 41 3) 41 div 10 (next smaller bank note) = 4 -> you need 4 10 dollar bank notes, rest is 1 4) there is no smaller bank note than 10 dollar but a remaining value of 1, so you cannot satisfy the order of 991 dollar w ith only 100, 50 and 10 dollar notes. Ciao, Mike

www.experts-exchange.com/viewQuestion.jsp?linkID=nIuEgduo9tbDQFA

1/3

11/13/11

Algorithm for bank notes : algorithm


05/25/00 02:45 AM, ID: 2844289

m art n_bannister: Mike, Thanks for the quick reply.

Was this comment helpful?

754 16.8K follow ers

Unfortunately, I think that your algorithm w on't w ork! Take as an example an order of 21 dollars w ith 5 and 2 dollar notes available. Your algorithm w ill subtract 5 from 21 until only 1 remains and then say that the order cannot be satisfied. Unfortunately it can! (3*5) + (3*2) Maybe I didn't understand. Please let me know if this is the case. /\/\artyn :-)}>-===

Like

6572 likes. Sign Up to see what our friends like.

ADVERTISEMENT

Print

Email

Save All Types

05/25/00 04:10 AM, ID: 2844524 Week Was this solution helpful? Month Year Overall 4,046,741

Lischke:

1. hanccocka

Aaah, yes, you are right. The algorithm is too greedy w hen subtracting the largest amount. It should be modified so: LN be the value of the currently largest available note AM be the current amount to be represented RM be the value of the difference of AM and x * LN, w here x is to be determined a div b is an integer division a mod b is a modulo operation function Split(AM, BankNotes): 1) LN = highest note in BankNotes 2) x = AM div LN 3) rest = AM - x * LN 4) if rest = 0 then result = true, exit 5) if number of BankNotes = 1 then result = false, exit 6) result = Split(rest, BankNotes - LN) 7) if result then exit 8) dec(x) 9) if x = 0 then result = false, exit 10) continue w ith 3) I cannot show that this algo w ill find the least amount of notes, but judging from the approach to exhaust the largest notes first I think it w ill (at least nearly). The algo does not show how to pass back the actual numbers of notes. You could use a structure or array for BankNotes w here you replace the note's values w ith their count, so the outer routine (the first caller of Split) w ill pass in values like: Split(21, [5, 2]) and w ill get back: Split(?, [3, 3]) Ciao, Mike

Member Profile 7,978 Expert Points Yesterday 2. leakim971 3. CodeCruiser 4. Ray_Pas 5. for_yan 6. capricorn1 7. kaufmed 8. alanhardisty 9. DaveBald 10. slightw v 11. johnb6767 12. demazter 13. mlmcc 14. matthew s 15. rorya 16. angelIII 17. w oolmilkp 18. HainKurt 19. DatabaseMX 20. sdstuber 21. arnold 22. ew angoya 23. dariusg 24. Siddharth 25. nobus 3,666,878 3,186,452 3,039,059 2,837,141 2,711,843 2,523,609 2,453,288 2,327,947 2,309,965 2,164,821 2,142,661 2,056,871 2,048,799 1,964,695 1,843,813 1,586,458 1,571,202 1,562,295 1,518,122 1,518,005 1,502,292 1,446,033 1,399,692 1,359,565

05/25/00 04:12 AM, ID: 2844534

Lischke: I should add:

Was this comment helpful?

- RM and mod operation aren't used, so ignore it (RM is actually "rest") - x is the value to be passed back in the place of LN (you need perhaps a running index to know the highest entry for each recursion level) Ciao, Mike

05/25/00 09:19 AM, ID: 2845994

m art n_bannister: Comment accepted as answ er

Was this comment helpful?

05/25/00 09:19 AM, ID: 2845995

www.experts-exchange.com/viewQuestion.jsp?linkID=nIuEgduo9tbDQFA

2/3

11/13/11
m art n_bannister: Mike,

Algorithm for bank notes : algorithm


Was this comment helpful?

Nice one! Looks like this method w ill crack it, as long as w e start from the beginning. If you are feeling spry, here is a further conundrum. How to do the above w hen some notes have already been allocated (by a percentage split) and yet they have not satisifed the order. e.g 1500 dollars has been allocated by percentages 2* 500, 2* 100, 2* 50 leaving 200. How do w e now use your algorithm to satisfy this order???? Any input greatly appreciated. /\/\

05/26/00 12:36 AM, ID: 2848653

Lischke:

Was this comment helpful?

You could make the outer function a bit smarter (the one w ich calls Split the first time) by subtracting the already allocated notes from the total amount and pass only the rest to Split along w ith the array of possible notes, w here the highest one must be less or equal to the rest amount. Ciao, Mike

20091007-EE-VQP-LI-5-LM

Shorten URL

Help

About Us

Contact Us

Terms of Use

EE Blog

Internet Rank

Privacy Policy

Site Map

E perts E change introduces JustDocuments.com

Just Documents

A simple way to store, access and share y our f iles.

Tr it for free!

www.experts-exchange.com/viewQuestion.jsp?linkID=nIuEgduo9tbDQFA

3/3