PAT B 1020 moon cake

The code was released, we learn together, help each other
Title:
Here Insert Picture Description
Input Sample:

3 20
18 15 10
75 72 45

Sample output:

94.50

Code below (Python):

species_number, demand = [int(x) for x in input().split()]  # 月饼种类数,需求量
inventory = [float(x) for x in input().split()]  # 各月饼库存
total_price = [float(x) for x in input().split()]  # 各月饼总售价
unit_price = [total_price[i]/inventory[i] for i in range(species_number)]  # 各月饼单价
sort_unit_price = sorted(unit_price, reverse=True)  # 新建一个降序的单价列表
biggest_gains = 0  # 最大收益
for i in sort_unit_price:  # 计算最大收益
    inv = inventory[unit_price.index(i)]
    if inv >= demand:
        biggest_gains += demand * i
        break
    else:
        biggest_gains += inv * i
        demand -= inv
print('%.2f' % biggest_gains)  #保留两位小数
Published 65 original articles · won praise 25 · views 1015

Guess you like

Origin blog.csdn.net/chongchujianghu3/article/details/105054020