余博文


私信TA

用户名:dotcpp0595286

访问量:990

签 名:

小余也有未来!!

等  级
排  名 1765
经  验 2560
参赛次数 0
文章发表 20
年  龄 0
在职情况 学生
学  校 长江大学
专  业

  自我简介:

TA的其他文章

解题思路:  非常标准的贪心问题

注意事项:

参考代码:

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 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区