码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 1070 Mooncake


    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region's culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

    Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 2 positive integers N (≤1000), the number of different kinds of mooncakes, and D (≤500 thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

    Sample Input:

    1. 3 200
    2. 180 150 100
    3. 7.5 7.2 4.5

    Sample Output:

    9.45

    贪心模拟,注意double类型的使用: 

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. struct Moon {
    6. double sum;
    7. double price;
    8. double avg;
    9. } a[1010];
    10. int n;
    11. double d;
    12. bool cmp(Moon m1, Moon m2) {
    13. return m1.avg > m2.avg;
    14. }
    15. int main() {
    16. scanf("%d %lf", &n, &d);
    17. for (int i = 0; i < n; i++) {
    18. scanf("%lf", &a[i].sum);
    19. }
    20. for (int i = 0; i < n; i++) {
    21. scanf("%lf", &a[i].price);
    22. a[i].avg = a[i].price / a[i].sum;
    23. }
    24. sort(a, a + n, cmp);
    25. int t = 0;
    26. double w = 0;
    27. while (d && t != n) {
    28. if (d - a[t].sum >= 0) {
    29. d -= a[t].sum;
    30. w += a[t].price;
    31. t++;
    32. } else {
    33. w += d / a[t].sum * a[t].price;
    34. d = 0;
    35. }
    36. }
    37. cout << setprecision(2) << fixed << w;
    38. return 0;
    39. }

    参考博客:【测试点2分析】:1020 月饼 (25分)(甲级 1070 Mooncake (25 分))_来老铁干了这碗代码的博客-CSDN博客

    scanf函数输入double类型需要注意的地方_wutieliu的博客-CSDN博客_double scanf 

  • 相关阅读:
    java加密,使用python解密 ,使用 pysm4 报 byte greater than 16的解决方法
    Python网络爬虫项目开发实战:如何解决验证码处理
    Redis基本数据类型
    网易有道开源语音合成引擎“易魔声”
    留学生作业Java程序帮做Javaweb帮做
    Node.js | 使用 zlib 内置模块进行 gzip 压缩
    @Transactional 的使用
    YOLOV7量化第二步: 模型标定
    人工智能在汽车业应用的五项挑战
    threejs视频教程学习(5):水天一色小岛
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126442910
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号