【python练习笔记】神秘的王宫

小偷来到了一个神秘的王宫,突然眼前一亮,发现5个宝贝,每个宝贝的价值都不一样,且重量也不一样,但是小偷的背包携带重量有限,所以他不得不在宝贝中做出选择,才能使偷到的财富最大,请你帮助小偷计算一下。

输入描述:

宝贝价值:6,3,5,4,6

宝贝重量:2,2,6,5,4

小偷背包容量:10

输出描述:

偷到宝贝的总价值:15

示例1

输入

6,3,5,4,6

2,2,6,5,4

10

输出

15

def situation(m, n):
    if n - m == 0:
        return 1
    loop = m
    p1 = 1
    p2 = 1
    while loop > 0:
        p1 *= n
        n -= 1
        loop -= 1
    while m > 0:
        p2 *= m
        m -= 1
    return int(p1 / p2)

def getMore():
    lv = [6, 3, 5, 4, 6]
    lw = [2, 2, 6, 5, 4]
    bag = 10
    v_max = 0
    maxArr = []
    length = len(lw)
    for i in range(2, length):
        for j in range(situation(i, length) ** 2):
            indexArr = sample(list(range(length)), k=i)
            w_sum = 0
            v_sum = 0
            for k in indexArr:
                w_sum += lw[k]
                v_sum += lv[k]
            if w_sum <= bag and v_sum > v_max:
                v_max = v_sum
                maxArr = indexArr
    return v_max, maxArr

for i in range(100):
    maxGet = getMore()
    print("maxGet-->", maxGet)

运行结果:

maxGet--> (15, [1, 0, 4])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [1, 0, 4])
maxGet--> (15, [0, 1, 4])
maxGet--> (15, [0, 4, 1])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [4, 0, 1])
maxGet--> (15, [4, 1, 0])
maxGet--> (15, [1, 4, 0])
maxGet--> (15, [0, 1, 4])

 

posted on 2019-03-31 20:40  smoggy  阅读(436)  评论(0编辑  收藏  举报