硬币题

list = (100, 50, 20, 10, 5, 2, 1)
result_dict = {}


def cache(func):
    def wrapper(*args):
        if args in result_dict:
            return result_dict[args]
        else:
            result = func(*args)
            result_dict[args] = result
            return result

    return wrapper


@cache
def method2(list1, sum):
    if len(list1) == 1:
        if sum % list1[0] == 0:
            return 1
        return 0
    else:
        max_coin = list1[0]  # 2等因子
        other_coins = list1[1:]
        num = 0
        result = 0
        while 1:
            if num * max_coin > sum:
                return result
            result += method2(other_coins, sum - num * max_coin)
            num += 1


if __name__ == '__main__':
    print(method2(list, 200))
    print(result_dict)

 

posted @ 2019-09-04 19:11  fjc0000  阅读(163)  评论(0编辑  收藏  举报