Pseudocode of an ATM that emits the smallest number of ballots possible

Asked

Viewed 204 times

1

Guys, I need to make a pseudocode that emits as few ballots as possible, for example: $377 3 banknotes of 100 1 note of 50 1 note of 20 1 note of 5 1 note of 2 please, anyone who can help, I’ll be very grateful!

  • Supposing or not there are always notes and money available ? In the realiadade to make one of the highest notes for the smallest , I’ve always been bad at making algorithms in a formal way , are always informal.

  • @Motta the only caution is that if you take a value like 11, you can’t make it from the biggest to the smallest, because if you take two of 5, one is missing, and you don’t have a grade of 1. you have to take one of 5 and three of 2. This type of situation (which can happen with the big notes too) is the only annoying part. Since it is not time-Critical, it is better to combine with the notes you have in the system, and for a higher score in the most valuable notes, for tiebreaker.

  • maybe this will help https://pt.wikipedia.org/wiki/Problema_da_backpack

2 answers

0

Guys thank you so much for the attention, I managed to do by the commands div and mod I will post here when I get home

  • Igor, you can post changing the answer that wrote the text, it would be nice to post and mark the answer if it was useful for you.

0

Igor, just check if the higher value of the ballot to be subtracted will give a negative value, if it does not, subtract, if it is negative, move to the next note of lower value.

Thus:

377 - 100 = 277 (is greater than zero)

277 - 100 = 177 (is greater than zero)

177 - 100 = 77 (is greater than zero)

77 - 100 = -33 (is less than zero, so I take the same value and subtract 50)

77 - 50 = 27 (is greater than zero)

27 - 50 = -23 (is less than zero, so I take the same value and subtract from 20)

27 - 20 = 7 (is greater than zero)

7 - 20 = -13 (is less than zero, so I take the same value and subtract from 10)

7 - 10 = -3 (is less than zero, so I take the same value and subtract from 5)

7 - 5 = 2 (is greater than zero)

2 - 5 = -3 (is less than zero, so I take the same value and subtract 2)

2 - 2 = 0 (null value, end of operation)

Adding up the times that each ballot was successfully used (resulted in a positive or null value), you get the result you want; three 100 banknotes, a 50 banknote, a 20 banknote, a 5 banknote and a 2 banknote.

In case of missing ballots of a certain value: Skip to the next of lower value and so on.

In case it ends in a value other than the value of one of the ballots (for example, 1 or 3), return to the first discounted value before the transaction results negative or arrives in value of a non-existent ballot.

Redo the transaction with the immediately lower ballot value and redo the calculation for the new ballot value by repeating this procedure until zero is reached.

For example 11:

11 - 10 = 1 (value less than the smallest ballot)

11 - 5 = 6 (value greater than zero)

6 - 5 = 1 (value less than the smallest ballot, for having used the value 5, step to use the value 2 6 - 2 = 4 (value greater than zero)

4 - 2 = 2 (value greater than zero)

2 - 2 = 0 (end of operation, a note of 5 and three notes of 2)

I hope I helped. If yes, please mark the answer.

Other examples:

To 8:

8 - 5 = 3 (5 is the lowest banknote value below 8)

3 - 5 = -2 (negative value, I should go back to 8 and take the next lowest value ballot)

8 - 2 = 6

6 - 2 = 4

4 - 2 = 2

2 - 2 = 0 (end of operation, four banknotes of 2)

To 101:

101 - 100 = 1 (value of nonexistent ballot, back to take the next of lower value)

101 - 50 = 51

51 - 50 = 1 (idem)

51 - 20 = 31

31 - 20 = 11

11 - 20 = -9 (caught the next of lowest value)

11 - 10 = 1 (value of nonexistent ballot, back to take the next of lower value)

11 - 5 = 6

6 - 5 = 1 (value of nonexistent ballot, back to take the next of lower value)

6 - 2 = 4

4 - 2 = 2

2 - 2 = 0 (end of operation, a 50 banknote, two 20 banknote, a 5 banknote and three 2 banknote)

  • And Igor? Served the answer friend? Mark if yes, please.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.