我在写knapsack
w = [0 for x in xrange(item_count+1)]
v = [0 for x in xrange(item_count+1)]
分别赋予值和权重(v,w)。创建了
c = [[0 for x in xrange(capacity+1)] for x in xrange(item_count+1)]
c,w和v分别在此之后分配(非零)
for i in range(1,item_count+1):
#for ever possible weight
for j in range(0,capacity+1):
#can we add this item to this?
if w[i] <= j :
c[i][j] = max(c[i-1][j], v[i] + c[i-1][j-w[i]])
else:
c[i][j] = c[i-1][j]
即使打印w[i] <= j:
似乎大于False
,w[i]
也始终为j
。不知道为什么?
答案 0 :(得分:2)
w [i]在代码中始终为0。初始化后从未触及