月饼

链接:https://www.nowcoder.com/questionTerminal/6fc9a928c7654b0fbc37d16b8bd29ff9
来源:牛客网
 

月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需
求量,请你计算可以获得的最大收益是多少。

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月饼,其库存量分别为18、15、10万吨,总售价分别为75、
72、45亿元。如果市场的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以及5万吨第3种月饼,获得
72 + 45/2 = 94.5(亿元)。

 

输入描述:

每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示月饼的种类数、以及不超过500(以万吨为单位)的正整数
D表示市场最大需求量。随后一行给出N个正数表示每种月饼的库存量(以万吨为单位);最后一行给出N个正数表示每种月饼的总售价(以亿
元为单位)。数字间以空格分隔。

输出描述:

对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后2位。

示例1

输入

3 20
18 15 10
75 72 45

输出

94.50

1.该代码只能通过一个测试用例,没有考虑库存为空的情况,空时需要优先售卖该月饼。

import operator
kind, total = map(float, input().split())
tons = list(map(float, input().split()))
tprice = list(map(float, input().split()))
price = {}
for i in range(int(kind)):
    try:
        price[i] = tprice[i]/tons[i]
    except:
        price[i] = 0
sortedprice = sorted(price.items(), key=operator.itemgetter(1), reverse=True)
# print(sortedprice)
stock = total
money = 0.00
for i in sortedprice:
    # print("i: ",i)
    tonsindex = i[0]
    iprice = i[1]
    # if stock == 0 or kind == 0:
    #     break
    # elif tons[tonsindex] > stock:
    if tons[tonsindex] >= stock:
        money += stock * iprice
        stock = 0
        break
    else:
        money += tprice[tonsindex]
        stock -= tons[tonsindex]

print("%.2f" %money)

该代码通过所有测试用例。 

kind, total = map(float, input().split())
stock = list(map(float, input().split()))
tprice = list(map(float, input().split()))
stock_price = zip(stock, tprice)
stock_price = sorted(stock_price, key=lambda x: x[1]/(x[0]+0.0001), reverse=True) 
#stock为零则优先卖该月饼
curstock = total
sum = 0.00
for i in stock_price:
    if curstock <=0 or kind == 0:
        break
    elif i[0] > curstock:
        sum += curstock*(i[1]/i[0])
        curstock -= curstock
    else:
        sum += i[1]
        curstock -= i[0]
print("%.2f" %sum)

#author:data挖掘机

tips:

1.sorted() 函数对所有可迭代的对象进行排序操作。

sort 与 sorted 区别:

sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作。

list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值,而内建函数 sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作。

语法

sorted 语法:

sorted(iterable, cmp=None, key=None, reverse=False)

参数说明:

  • iterable -- 可迭代对象。
  • cmp -- 比较的函数,这个具有两个参数,参数的值都是从可迭代对象中取出,此函数必须遵守的规则为,大于则返回1,小于则返回-1,等于则返回0。
  • key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  • reverse -- 排序规则,reverse = True 降序 , reverse = False 升序(默认)。

返回值

返回重新排序的列表。

#1 对list进行排序,key为下标建立字典
import operator
price = list(map(folat, input().split()))
stock_price = sorted(price, key = operator.itemgetter(1), reverse = True)

#2 #按照元素排序,生成存储元组的列表 赋予key
dict1 = {}
dict1[i]赋值
dict2 = sorted(dict.items(), key=operator.itemgetter(1),reverse=True)


#3 建立字典stock_price,按照单价排序
stock = list(map(float, input().split()))
tprice = list(map(float, input().split()))
stock_price = zip(stock, tprice)
stock_price = sorted(stock_price, key=lambda x: x[1]/(x[0]+0.0001), reverse=True) 
#stock为零是优先卖该月饼

2.本题可以用结构体存储月饼的库存,总价和单价,然后按照单价排序,不用记录下标,因为同一个结构体内即可获取月饼的所有信息。上面#3中的思想类似于结构体,存储月饼的总结和库存,计算单价对字典进行排序。

3.print("%.2f" %sum)格式化输出,保留两位小数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值