Python Algorithms - Greedy -"Coin Machine"

Sunmi Shin·2022년 11월 3일

Python Algorithms

목록 보기
1/7
post-thumbnail

Problem:
The coin machine will give you the currency in the least amount of coins.

최소의 동전 개수로 거스름돈을 거슬러 주는 머신이 있다.

#Define the bill('money') and the default amount of coins('count').
money = 1260
count = 0

#Define a list of coin types.
coin_types = [500, 100, 50, 10]

#During a loop, the machine will give you coins 
#in a descending order 
#and record the number of coins in the 'count' variable.
for coin in coin_types:
    count += (money // coin)
    money %= coin

#print the answer.
print(count)
profile
Tempest Data Analyst

0개의 댓글