Pseudocode of an ATM that issues the smallest number of banknotes possible

1

Speak, I need to make a pseudocode that issues the smallest number of possible ballots, for example: $ 377 3 ratings out of 100 1 note of 50 1 note of 20 1 note of 5 1 note of 2 Please, whoever can help me, I will be very grateful!

    
asked by anonymous 02.06.2016 / 03:31

2 answers

0

Igor, just check if the highest value of the ballot to be subtracted will give a negative value, if not, subtract, if negative, go to the next lowest note.

So:

377 - 100 = 277 (is greater than zero)

277 - 100 = 177 (is greater than zero)

177 - 100 = 77 (is greater than zero)

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

77 - 50 = 27 (is greater than zero)

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

27 - 20 = 7 (is greater than zero)

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

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

7 - 5 = 2 (is greater than zero)

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

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

In addition to the fact that each ballot has been used successfully (it has resulted in a positive or zero value), one arrives at the desired result; three 100 notes, a 50 note, a 20 note, a 5 note, and a 2 note .

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

In case you end up with a value other than the value of one of the bills (for example, 1 or 3), go back to the first discounted amount before the operation yields a negative result or reaches a nonexistent amount.

Repeat the operation with the value of the immediately lower ballot and redo the calculation for the new ballot value by repeating this procedure until it reaches zero.

For example 11:

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

11 - 5 = 6 (value greater than zero)

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

4 - 2 = 2 (value greater than zero)

2 - 2 = 0 (end of operation, 5 and 3 notes of 2 )

I hope I have helped. If so, please check the answer.

Other examples:

For 8:

8 - 5 = 3 (5 is the smallest ballot value below 8)

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

8 - 2 = 6

6 - 2 = 4

4 - 2 = 2

2 - 2 = 0 (end of operation, 4 notes of 2 )

For 101:

101 - 100 = 1 (non-existent ballot value, I return to take the next one of lower value)

101 - 50 = 51

51 - 50 = 1 (idem)

51 20 = 31

31 - 20 = 11

11 - 20 = -9 (I get the next one of the lowest value)

11 - 10 = 1 (non-existent banknote value, I return to take the next one of lower value)

11 - 5 = 6

6 - 5 = 1 (non-existent ballot value, I return to take the next one of lower value)

6 - 2 = 4

4 - 2 = 2

2 - 2 = 0 (end of operation, one note of 50, two notes of 20, one note of 5 and three notes of 2

)

>     
02.06.2016 / 04:35
0

Well thanks for the attention, I managed to do the div and mod commands I'll post here when I get home

    
02.06.2016 / 18:26