Love丶FFC's Blog

PAT乙级:1020 月饼

2020-02-11 09:07:52
阅读:1081   •   评论:19
标签:,

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

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

输入格式:

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

输出格式:

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

输入样例:

3 20
18 15 10
75 72 45

输出样例:

94.50

编程语言:Python

解题思想:

1.贪心算法的思想,根据月饼的单价进行降序排序

2.对排序好的月饼列表进行逐个判断

3.注意输入时不能用int,需要用float

4.使用lambda函数、%.2f等用法

labmda函数:https://www.cnblogs.com/lovewhale1997/p/11424429.html

%.2f %x的用法:https://blog.csdn.net/imredboy/article/details/99189030

时间复杂度:O(N)

代码如下:

  1. NOM, Requirement = map(int, input().split()) # 输入月饼种类数、总需求量(万吨)
  2. Stock = list(map(float, input().split())) # 各个月饼的库存(万吨)
  3. Price = list(map(float, input().split())) # 各个月饼的总售价(亿)
  4. MoonCake = [[0, 0, 0] for i in range(NOM)] # 创建月饼列表,存放各个月饼的总库存、总售价、售价/库存的比值
  5. SaleCount = 0 # 最终卖出的总价
  6.  
  7. for i in range(NOM):
  8. MoonCake[i] = [Stock[i], Price[i], Price[i] / Stock[i]]
  9.  
  10. MoonCake.sort(key=lambda x: x[2], reverse=True) # 使用labmda函数作为降序排序参数
  11. # 在sort方法的key中使用labmda x:x[2]等同于使用MoonCake列表的第二个元素进行排序
  12.  
  13. i = 0
  14. while Requirement > 0 and i < NOM: # 仍有需求且月饼仍有库存
  15. if Requirement > MoonCake[i][0]: # 当前需求>当前月饼总库存
  16. Requirement -= MoonCake[i][0]
  17. SaleCount += MoonCake[i][1]
  18. i += 1
  19. elif Requirement <= MoonCake[i][0]: # 当前需求<=当前月饼总库存
  20. SaleCount += Requirement * MoonCake[i][2]
  21. Requirement = 0
  22.  
  23. print('%.2f' % SaleCount)

评论板

共有 19 条评论

  1. engarma

    Colton AFKcgFxYbg 6 27 2022 buy priligy 30mg

  2. original louis vuittons outlet

    I just wanted to thank you for the fast service. quite possibly they look great. I received them a day earlier than expected. like I will definitely continue to buy from this site. in any event I will recommend this site to my friends. Thanks!
    original louis vuittons outlet https://www.cheapreallouisvuitton.com/

  3. authentic louis vuitton outlet

    I just wanted to thank you for the fast service. or possibly a they look great. I received them a day earlier than expected. love the I will definitely continue to buy from this site. in any event I will recommend this site to my friends. Thanks!
    authentic louis vuitton outlet https://www.louisvuittonsoutletstore.com/

  4. authentic cheap jordans

    I just wanted to thank you for the fast service. aka they look great. I received them a day earlier than expected. choose to I will definitely continue to buy from this site. no matter what I will recommend this site to my friends. Thanks!
    authentic cheap jordans https://www.realcheapretrojordanshoes.com/

  5. cheap louis vuitton handbags

    I just wanted to thank you for the fast service. along with they look great. I received them a day earlier than expected. particularly I will definitely continue to buy from this site. in any event I will recommend this site to my friends. Thanks!
    cheap louis vuitton handbags https://www.bestlouisvuittonoutlet.com/

  6. cheap jordans for sale

    Read reviews and was a little hesitant since I had already inputted my order. or even a but thank god, I had no issues. the same as received item in a timely matter, they are in new condition. regardless so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans for sale https://www.cheapsneakeronline.com/

  7. louis vuitton outlet sale online

    Read reviews and was a little hesitant since I had already inputted my order. or it may be but thank god, I had no issues. which includes received item in a timely matter, they are in new condition. in either case so happy I made the purchase. Will be definitely be purchasing again.
    louis vuitton outlet sale online https://www.louisvuittonsoutletonline.com/

  8. cheap jordans online

    Read reviews and was a little hesitant since I had already inputted my order. possibly but thank god, I had no issues. the same as received item in a timely matter, they are in new condition. an invaluable so happy I made the purchase. Will be definitely be purchasing again.
    cheap jordans online https://www.realjordansshoes.com/

  9. Preliaf

    Anemia, thyroid dysfunction, symptoms of obstructive sleep apnea, and depression are frequently part of the evaluation revatio vs viagra 5 5 mg L for 5 hours, daily for 7 days

  10. Preliaf

    415 ATORVASTATINA OPHALAC Atorvastatina 10 mg Caja x 10 tabs levitra prix en bretagne But we are still very worried fo course wont find out till 20 week scan if bump caught it too a long wait im, 10 weeks on wed

  11. used gucci bags ebay

    As the evergreen tree of Gucci, the Bacchus bag is also very popular. Today, I will show you the authenticity and fake identification points of Gucci~

  12. Retro Air Jordan Shoes

    Cheap Jordans 5 (V) Retro Fire Red Women

  13. gucci shoulder handbags

    Jo Ellison is one of the many fans of this unique bag. When the bag itself is complicated enough, our clothing must be very simple and clean. Alison is very unique with a gray suit and dark brown double-sided woolen coat for layering. The combination of neutral color and earth color is very harmonious, and the color is just in line with the composition of the bag, which is very carefully matched. ▼

  14. cheap air jordans

    In terms of color matching, it combines brilliant red, white, and dark gray, and the overall visual effect is very eye-catching.

  15. vOekbmL

    levitra 5 mg precio Since its approval in 1977, tamoxifen has been used to treat millions of women and men diagnosed with hormone receptor positive breast cancer

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:416997