解题思路: 非常标准的贪心问题
注意事项:
参考代码:
k = int(input()) for _ in range(k): w = int(input()) # 口袋承重上限 s = int(input()) # 种类数 list = [int(i) for i in input().split()] weight = [] # 每种金属的质量 price = [] # 每种金属的单价 for i in range(0,2*s,2): weight.append(list[i]) price.append(list[i+1] / list[i]) value = 0 ws = w # 剩余承重 while ws != 0: if len(price) == 0: break maxprice = max(price) maxindex = price.index(maxprice) maxweight = weight[maxindex] if ws - maxweight >= 0: ws -= maxweight value += maxprice * maxweight price.pop(maxindex) weight.pop(maxindex) else: value += maxprice * ws ws = 0 print('{:.2f}'.format(value))
0.0分
2 人评分
点我有惊喜!你懂得!浏览:1402 |
陶陶摘苹果 (C语言代码)浏览:1605 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:1515 |
大神老白 (C语言代码)浏览:642 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:482 |
WU-蓝桥杯算法提高VIP-Quadratic Equation (C++代码)浏览:1747 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:619 |
C语言程序设计教程(第三版)课后习题10.1 (C语言代码)浏览:530 |
川哥的吩咐 (C语言代码)浏览:611 |
Quadratic Equation (C语言代码)浏览:988 |