main4() {int m,n, i, j,w[50],[50],p1[50],b[50],s,max;input(m,n);for(i=1,s=0;i<=n;i=i+1){input(w[i],p[i]);p1[i]=p[i];s=s+w[i];} if(s<=m) {print("whole choose");return;} for(i=1;i<=n;i=i+1) {max = 1; for(j=2;j<=n;j=j+1) if (pl[j]/w[j]> p1[max]/w[max]) max = j; pl[max]=0; b[i]= max;} for(i=l,s=0;s<m and i<=n;i=i+1)s=s+w[b[i]];if(s<>m) w[b[i-1]]=m-(s-w[b[i-1]]);for(j=1;j<=i-1;j=j+1) print("choose",[j],"weight",w[b[j]]);}转换成python语言的代码
时间: 2024-03-30 11:36:30 浏览: 65
def main4():
m, n = map(int, input().split())
w = [0] * 50
p = [0] * 50
p1 = [0] * 50
b = [0] * 50
s = 0
for i in range(1, n + 1):
w[i], p[i] = map(int, input().split())
p1[i] = p[i]
s += w[i]
if s <= m:
print("whole choose")
return
for i in range(1, n + 1):
max_idx = 1
for j in range(2, n + 1):
if p[j] / w[j] > p1[max_idx] / w[max_idx]:
max_idx = j
p[max_idx] = 0
b[i] = max_idx
i = 1
s = 0
while s < m and i <= n:
s += w[b[i]]
i += 1
if s != m:
w[b[i - 1]] = m - (s - w[b[i - 1]])
for j in range(1, i):
print("choose", b[j], "weight", w[b[j]])
阅读全文