来个 Python AC的 coding=utf-8 def bag(n, c, w, v): res = [[-1 for j in range(c + 1)] for i in range(n + 1)] for j in range(c + 1): res[0][j] = 0 for i in range(1, n + 1): for j in range(1, c + 1): res[i][j] = res[i - 1][j] if j >= w[i - 1] and res[i][j] < res[i - 1][j - w[i - 1]] + v[i - 1]: res[i][j] = res[i - 1][j - w[i - 1]] + v[i - 1] return res def show(n, c, w, res): x = [0 for i in range(n)] j = c for i in range(n,0,-1): if res[i][j] > res[i - 1][j]: x[i - 1] = 1 j -= w[i - 1] for i in range(n): if x[i]==1: print(i+1), if name == 'main': c = int(raw_input()) w = raw_input() w = w.split() for i in range(len(w)): w[i] = int(w[i]) v = raw_input() v = v.split() for i in range(len(v)): v[i] = int(v[i]) n = len(v) res = bag(n, c, w, v) show(n, c, w, res)
点赞 评论

相关推荐

10-24 13:36
门头沟学院 Java
Zzzzoooo:更新:今天下午有hr联系我去不去客户端,拒了
点赞 评论 收藏
分享
10-13 17:47
门头沟学院 Java
wulala.god:图一那个善我面过,老板网上找的题库面的
点赞 评论 收藏
分享
牛客网
牛客企业服务