码农知识堂 - 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 

  • 相关阅读:
    第09章 性能分析工具的使用【2.索引及调优篇】【MySQL高级】
    汽车标定技术(六)--基于模型开发如何生成完整的A2L文件(2)
    【ros2 control 机器人驱动开发】双关节多控制器机器人学习-example 6
    终于有人把Java程序员必学知识点整理出来了,令人有如醍醐灌顶
    Java-IO流之字节流(上篇)
    【RocketMQ 一】MQ概述
    idea 集成 git 后使用的常用命令
    qt 6知识集
    Java BIO基本介绍
    【业务功能篇99】微服务-springcloud-springboot-电商订单模块-生成订单服务-锁定库存
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126442910
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号