Greedy method - change for change problem

Problem Description: We have 5, $ 1, $ 0.50 0.2 yuan, 0.1 yuan, 0.05 yuan banknote, how to minimize the change of the number of

The basic idea; the bills are sorted in descending order, first find out as much as possible of large;

coins = [2,1,0.5,0.2,0.1,0.05]
money = 5.65
def coinChange(coins,money):
    count = 0
    i = 0
    res=[]
    while i<len(coins):
        if money>=coins[i]:
            n=int(money/coins[i])
            for _ in range(n):
                res.append(coins[i])
            count+=n
            change=n*coins[i]
            money=money-change
        i+=1
    return count,res

Guess you like

Origin www.cnblogs.com/xiximayou/p/11925294.html