下面是計算給定金額變化的代碼: 但是,它並沒有寫出最低數量的硬幣用於更改數量,但代碼似乎給出了所需的最小數量的硬幣。我想要一個沒有給出所需硬幣數量的情況。需要一個測試用例,其中給出的最小數量的硬幣代碼在Python中失敗?
def change(amount):
money =()
for coin in [25,10,5,1]:
num = amount/coin
money += (coin,) * num
amount -= coin * num
return money
print change(59)
output is:
(25, 25, 5, 1, 1, 1, 1)
是什麼讓你覺得它可能不會給最小數量的硬幣? –