解题思路:跟金银岛一样,https://blog.dotcpp.com/a/98826
注意事项:
参考代码:
n,w = map(int,input().split()) gi = [] # 重量 pi = [] # 直接存单价不存总价,float for _ in range(n): x,y = map(int,input().split()) gi.append(x) pi.append(y/x) value = 0 while w != 0 : if len(pi) == 0: break maxprice = max(pi) maxindex = pi.index(maxprice) maxweight = gi[maxindex] if w - maxweight >= 0 : w -= maxweight value += maxweight * maxprice pi.pop(maxindex) gi.pop(maxindex) else: value += w * maxprice w = 0 print('{:.1f}'.format(value))
0.0分
1 人评分