0

Yes I know there has been similar posts to this however after looking through them all I'm still stuck as I'm very new to programming and none of the answers given were specific enough to my problem to help.


Question. Write an efficient ACL (algorithmic computer language) algorithm that, given a cost of an item (less than or equal to one dollar), gives the number of 50 cent, 20 cent, 10 cent, 5 cent and 1 cent coins the buyer would receive if they handed over one dollar. You must minimise the number of coins in the change.


The question is not related to any specific programming language, and the answer can only use simple ACL language like if, if-else, while loops and can't use arrays or other advanced commands.

Here is where I am at:

Algorithm minimum amount of change

{
    int cost, fifty, twenty, ten, five, one;
    
    fifty = 0;
    twenty = 0;
    ten = 0;
    five = 0;
    one = 0;
    
    read (cost);
    if (cost <= 50)
    {
        fifty = 1;
    

Finished code, thankyou for your help! If you can see any ambiguities or can help me simplify the code please let me know.


Algorithm how much change
{
    int cost, change, fifty, twenty, ten, five, one;
    
    fifty = 0;
    twenty = 0;
    ten = 0;
    five = 0;
    one = 0;
    
    read (cost);
    change = 100 - cost;
    
    if (change >= 50)
    {
        fifty = fifty + 1;
        change = change - 50;
    }
    while (change >= 20)
    {
        twenty = twenty + 1;
        change = change - 20;
    }
    while (change >= 10)
    {
        ten = ten + 1;
        change = change - 10;
    }
    while (change >= 5)
    {
        five = five + 1;
        change = change - 5;
    }
    while (change >= 1)
    {
        one = one + 1;
        change = change - 1;
    }
    
    print(one, five, ten, twenty, fifty);
}
  • 20 cent? Out of curiosity, what currency are we working with here? I'm not sure who has 20 cent pieces. Or is that value just hypothetical? (Or is it a typo) – nzifnab Mar 08 '11 at 07:43
  • 2
    A lot of currencies do afaik. Most currencies come in those proportions (1-2-5), and you either have 5-10-25-50-100-250-500 coins, or 1-2-5-10-20-50-100-200 coins. At least the Euro has the latter configuration. – markijbema Mar 08 '11 at 07:46
  • @nzifnab: Probably Euro. – Björn Pollex Mar 08 '11 at 07:50
  • I'm Australian we have 5c 10c 20c 50c $1 and 2$ coins. Thanks to all posts so far, sorry I'm such a noob, I only decided to do a programming course as an elective at uni so I have no experience what so ever sorry. – Callum Woodward Mar 08 '11 at 08:01
  • 1
    Pretty much everyone except the US has 20c pieces. – Nick Johnson Mar 08 '11 at 17:30

5 Answers5

2

actually, cost >= 50 need to be checked only once, but this is more generic (for cases over 1 dollar)

while (cost >= 50)
{
fifty++;
cost -= 50;
}
while (cost >= 20)
{
twenty++;
cost -=20;
}
...
MByD
  • 135,866
  • 28
  • 264
  • 277
  • 1
    I would prefer `fifty = floor(cost/50)` etc. – Björn Pollex Mar 08 '11 at 07:49
  • Thanks you MByD, had to google what ++ and -= were :S cause we have not learnt that, and can only use in our answers what we have learnt. I posted my finished code in the answer if you can see any ambiguities please let me know thanks. – Callum Woodward Mar 08 '11 at 08:35
  • Actually, I would recommend using the method Space_C0wb0y suggested: `fifty = cost / 50; cost = cost % 50; twenty = cost / 20; cost = cost % 20;` etc. when % gives the remainder. – MByD Mar 08 '11 at 08:54
  • For this part of that statement 'fifty = cost/50' as fifty is an integer will this be a problem because the answer to that statement would more then likely be a decimal? – Callum Woodward Mar 08 '11 at 09:41
  • in most languages, the division 80 /50 will return 1, you may also use floor(cost/50) as Space_C0wb0y suggested to get it. – MByD Mar 08 '11 at 09:46
2

For your particular change amounts, I believe a simple greedy naive "pick the largest until I can't anymore" strategy will work.

ex.:

  1. How many 50's can I pick before that amount of 50's is greater than the change amount? Keep track of that number of 50's, subtract that amount from the total
  2. Repeat for 20's, 10's, etc.

However, it does not always follow that the greedy method will work. For a "general" solution, see Coin Change - Algorithmist

helloworld922
  • 10,801
  • 5
  • 48
  • 85
  • Can you explain how you conclude this is NP-complete? – Björn Pollex Mar 08 '11 at 07:48
  • Oops, I think I must be tired (1am isn't that late, is it?) cause what I meant was this was a case where a greedy method would work. Changed post to reflect this. – helloworld922 Mar 08 '11 at 07:55
  • 1
    According to [Wolfram](http://mathworld.wolfram.com/CoinProblem.html) this problem is only proven to be NP-Hard, not complete. – Björn Pollex Mar 08 '11 at 07:59
  • @Space_C0wb0y The NP-Hard problem is finding the largest number that cannot be represented with the given coin denominations, something that never happens if `1` is included. That said, I haven't seen a proof that greedy is optimum for any set of denominations. – Apalala Mar 08 '11 at 16:15
1

Since this sounds like homework I'm not going to give the answer straight away, but give you a hint in the right direction.

Let's say you are some point in your program. You have a certain amount left to return, let's say 80 cents. How would you go about getting one coin which you certainly must return?

markijbema
  • 3,985
  • 20
  • 32
1

Hint: Start with the largest coin and find out the max # of those you can use, then go to the second largest coin and repeat. It looks like they made it easier on you by using twenty coin instead of 25 lol.

user623879
  • 4,066
  • 9
  • 38
  • 53
0

enter image description here

more detailed:

enter image description here

and even a brute force algorithm which is O(d M^d)

enter image description here
This is from Max Alekseyev, University of South Carolina cse.

edgarmtze
  • 24,683
  • 80
  • 235
  • 386