Decompose the value of a Python banknote

Asked

Viewed 1,866 times

0

I need to read an integer value and calculate the fewest possible banknotes (ballots) in which the value can be decomposed. Banknotes considered are 100, 50, 20, 10, 5, 2 and 1.

And dps print the read value and then the minimum amount of notes of each type required.

EXAMPLE:

  • entree

576

  • exit

R$ 576

5 banknote(s) of R$ 100,00

1 note(s) of R$ 50,00

1 note(s) of R$ 20,00

0 banknote(s) of R$ 10,00

1 note(s) of R$ 5,00

0 banknote(s) of R$ 2,00

1 banknote(s) of R$ 1,00

1 answer

1


I believe this solves, as the python was not specified I used python 2.7 but this can be easily converted to 3.x

N = int(input()) 

notas = [100.00,50.00,20.00,10.00,5.00,2.00,1.00]
print ("R$ %d:"%(N))
for x in notas:
    print ("%i nota(s) de R$ %.2f"%((N/x),x))
    N %= (x)

Edit: The multiplication is due to the division of python that can round a little differently for floats , I arranged better for integers

Edit2: Code for python 3.x

Edit3: I had forgotten the 1 real note

Edit4: Using comma instead of stitch would look like this:

N = int(input()) 

notas = [100,50,20,10,5,2,1]
print ("R$ %d:"%(N))
for x in notas:
    print ("%i nota(s) de R$ %d,00"%((N/x),x))
    N %= (x)

Browser other questions tagged

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