贪心算法之找零钱

def greedy_change(amount, coins):
    coins.sort(reverse=True)  # 将硬币按面额从大到小排序
    change = []
    for coin in coins:
        while amount >= coin:
            amount -= coin
            change.append(coin)  # 将硬币加入到找零列表中
    return change

s = greedy_change(10,[1,2,3])
print(s)

 

posted @ 2023-11-01 09:26  年轻人——001  阅读(32)  评论(0编辑  收藏  举报