1. 程式人生 > >【python練習筆記】神秘的王宮

【python練習筆記】神秘的王宮

筆記 財富 max class ati pytho index more amp

小偷來到了一個神秘的王宮,突然眼前一亮,發現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])

【python練習筆記】神秘的王宮