解题思路: 非常标准的贪心问题
注意事项:
参考代码:
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 人评分
C语言程序设计教程(第三版)课后习题11.5 (C语言代码)浏览:967 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:508 |
最小公倍数 (C语言代码)浏览:862 |
Pascal三角 (C语言代码)格式错误浏览:516 |
C语言程序设计教程(第三版)课后习题6.6 (C语言代码)浏览:624 |
【蟠桃记】 (C语言代码)浏览:664 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:398 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:575 |
简单的a+b (C语言代码)浏览:506 |
矩阵转置 (C语言代码)浏览:782 |