懵懂的菜鸟

Stay hungry,Stay foolish.

导航

最小背包问题

 1 #coding = utf-8
 2 import sys
 3 '''
 4 此问题可以用0-1背包进行求解
 5 '''
 6 
 7 def Cal_Trans(a):
 8     r=1
 9     for i in range(6):
10         if i<4:
11             r=r+a[i]*a[i]
12             if r>36:
13                 r=r+1
14         else:
15             r=r+a[i]*a[i]
16             if r>36:
17                 r=r+1
18             if a[i]>1:
19                 r=r+a[i]
20     return r
21 
22 if __name__=='__main__':
23     b=1
24     i=0
25     min=[]
26     while(b>0):
27         a=[int(i) for i in sys.stdin.readline().split(' ')]
28 
29         b=sum(a)
30         if(b>0):
31             min.append(Cal_Trans(a))
32             i=i+1
33         else:
34             break
35     print(min)
36     print(type(min))
37     for j in range(len(min)):
38         print(str(min[j]))

 

posted on 2017-08-28 20:38  懵懂的菜鸟  阅读(864)  评论(0编辑  收藏  举报