zoukankan      html  css  js  c++  java
  • 动态规划

    题目:从面额为2,3,7的钱币中,找出能凑够100块的最小钱币数。

    思考



    import time
    money_type = [2, 3, 7]
    total = 100
    ret = {}
    st = time.time()
    for i in range(1, total + 1):
        tmp = []
        for mt in money_type:
            if mt == i:
                tmp.append((1, [mt]))
            else:
                if i - mt in ret:
                    count, zuhe = ret[i - mt]
                    count += 1
                    zuhe = zuhe[:]
                    zuhe.append(mt)
                    tmp.append((count, zuhe))
                if mt == i:
                    tmp.append((1, [mt]))
        if tmp:
            ret[i] = min(tmp)
    print(i, ret.get(i, ()))
    print(f"total time {time.time() - st}")
    import sys
    sys.setrecursionlimit(1000000)
    st = time.time()
    # 递归求导,100时,递归求导极慢
    def my_func(total, zuhe=[]):
        if total in money_type:
            zuhe.append(str(total))
            print("可能组合为:"+",".join(zuhe))
            return 1
        tmp = []
        for mt in money_type:
            rest = total - mt
            if rest >= min(money_type):
                new_zuhe = zuhe[:]
                new_zuhe.append(str(mt))
                count = my_func(rest, new_zuhe) + 1
                tmp.append(count)
            else:
                continue
        if tmp:
            return min(tmp)
        else:
            return 0
       
    
    print("最小取值为"+ str(my_func(total)))
    print(f"total time {time.time() - st}")  
        
          
    
    
        
    
  • 相关阅读:
    PHP基础介绍
    day96
    day95
    day94
    day93
    day93之微信推送
    22个必须知道的css技巧
    利用Js或Css滤镜实现IE6中PNG图片半透明效果 IE6PNG妥妥的
    dedecms调用日期格式化形式大全
    innerHTML动态添加html代码和脚本兼容性问题处理方法
  • 原文地址:https://www.cnblogs.com/linyihai/p/10427406.html
Copyright © 2011-2022 走看看