利用递归求解贩卖机找零最优解

def rec(coinvaluelist,change):
    mincoins=change
    if change in coinvaluelist:
        return 1
    else:
        for i in [c for c in coinvaluelist if c<=change]:
            numcoins=1+rec(coinvaluelist,change-i)
            if numcoins<mincoins:
                mincoins=numcoins
        return mincoins
print(rec([1,5,10,25],63))
    原文作者:算法
    原文地址: https://www.twblogs.net/a/5bd3be512b717778ac20c737
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞