1020 月饼 (25分)

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

注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 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

贪心算法嗨起来

第一版(答案错误+运行超时):

package base;import java.math.BigDecimal;
import java.util.*;public class Main1020 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int totalNum = scanner.nextInt();Double d = scanner.nextDouble();String kc = scanner.nextLine();String shell = scanner.nextLine();String[] kc1 = kc.trim().split(" ");String[] money = shell.trim().split(" ");double totalMoney = 0.0;HashMap<Integer, Double> map = new HashMap<>();for (int i = 0; i < kc1.length; i++) {Integer integer = new Integer(kc1[i]);Double goodsMoney = new Double(money[i]);BigDecimal bg = new BigDecimal((goodsMoney / integer));double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();map.put(i, f1);}List<Map.Entry<Integer, Double>> list = new ArrayList<Map.Entry<Integer, Double>>(map.entrySet());Collections.sort(list, new Comparator<Map.Entry<Integer, Double>>() {//升序排序@Overridepublic int compare(Map.Entry<Integer, Double> o1,Map.Entry<Integer, Double> o2) {return o1.getValue().compareTo(o2.getValue());}});for (int i = list.size() - 1; i >= 0; i--) {Map.Entry<Integer, Double> integerIntegerEntry = list.get(i);Integer key = integerIntegerEntry.getKey();Integer integer = new Integer(kc1[key]);Integer integer1 = new Integer(money[key]);if (d - integer > 0) {d = d - integer;totalMoney = totalMoney + integer1;} else {totalMoney = totalMoney + (integerIntegerEntry.getValue() * d);break;}}System.out.println(String.format("%.2f", totalMoney));}}

第二版(部分正确+运行超时+一个答案错误 15分):

 public static void main(String[] args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String line=br.readLine();String[] a=line.split(" ");int totalNum=Integer.parseInt(a[0]);double d=Integer.parseInt(a[1]);String kc = br.readLine();String shell = br.readLine();String[] kc1 = kc.trim().split(" ");String[] money = shell.trim().split(" ");double totalMoney = 0.0;HashMap<Integer, Double> map = new HashMap<>();for (int i = 0; i < kc1.length; i++) {Integer integer = new Integer(kc1[i]);Double goodsMoney = new Double(money[i]);BigDecimal bg = new BigDecimal((goodsMoney / integer));double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();map.put(i, f1);}List<Map.Entry<Integer, Double>> list = new ArrayList<Map.Entry<Integer, Double>>(map.entrySet());Collections.sort(list, new Comparator<Map.Entry<Integer, Double>>() {//升序排序@Overridepublic int compare(Map.Entry<Integer, Double> o1,Map.Entry<Integer, Double> o2) {return o1.getValue().compareTo(o2.getValue());}});for (int i = list.size() - 1; i >= 0; i--) {Map.Entry<Integer, Double> integerIntegerEntry = list.get(i);Integer key = integerIntegerEntry.getKey();Integer integer = new Integer(kc1[key]);Integer integer1 = new Integer(money[key]);if (d - integer > 0) {d = d - integer;totalMoney = totalMoney + integer1;} else if (d - integer == 0) {totalMoney = totalMoney + integer1;break;} else {totalMoney = totalMoney + (integerIntegerEntry.getValue() * d);break;}}System.out.println(String.format("%.2f", totalMoney));}

把Scanner换成了BufferedRead然后谜一样的事情就出现了= =,原先是答案错误,现在变成了部分正确加超时??????????魔鬼系统在线变脸?????

他人代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;public class Main {public static void main(String[] args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String line=br.readLine();String[] a=line.split(" ");int num=Integer.parseInt(a[0]);double max=Integer.parseInt(a[1]);MoonCake[] cakes=new MoonCake[num];double maxmoney=0.0;String[] kucun=br.readLine().split(" ");String[] price=br.readLine().split(" ");for(int i=0;i<num;i++){MoonCake cake=new MoonCake();cake.setKucun(Double.parseDouble(kucun[i]));cakes[i]=cake;}for(int i=0;i<num;i++){cakes[i].setPrice(Double.parseDouble(price[i]));}Arrays.sort(cakes, new Comparator<MoonCake>() {public int compare(MoonCake o1, MoonCake o2) {double x1=o1.getPrice()/o1.getKucun();double x2=o2.getPrice()/o2.getKucun();return x1>x2?1:-1;}});int j=num-1;while(max>0 && j>=0){if(max>cakes[j].getKucun()){max=max-cakes[j].getKucun();maxmoney+=cakes[j].getPrice();}else{maxmoney+=(max/cakes[j].getKucun())*cakes[j].getPrice();max=0;}j--;}System.out.println(String.format("%.2f",Math.round(maxmoney*100)/100.0));}static class MoonCake{private double kucun;private double price;public double getKucun() {return kucun;}public void setKucun(double kucun) {this.kucun = kucun;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}}
}

PAT JAVA 乙 1020 吃月饼相关推荐

  1. pat 乙级 1020 吃月饼(25)

    总结一下 :(因为涉及到 小数计算 所以 类型 我选择了double) 第一次 提交 :测试点3 无论如何都过不去 . 最后 发现 忽略 两种情况 : 第一 :  所有 库存都满足 不了需求 比如  ...

  2. 除了吃月饼,中秋节还能干啥?

    明天 八月十五,团圆夜 花好月圆之际 除了吃月饼,还能干啥? 阿里妹带来双重好礼,陪你过中秋~ (往下看,送云栖大会三日通票哦) 1重礼  - 阿里技术热门精选 - <Java 开发手册> ...

  3. OSChina 周三乱弹 ——我就爱吃月饼,就爱抢月饼。我高兴。

    2019独角兽企业重金招聘Python工程师标准>>> @巴拉迪维:张敬轩<吻得太逼真>. 巴叔是麦霸, 爱上了乱弹点歌. 吻得太逼真 - 张敬轩 手机党少年们想听歌,请 ...

  4. 中秋将至,想吃月饼了吗?

    过几天就是中秋佳节了,不知道大家有什么打算呢?回家?旅游?聚会?想来还是回家团圆的人居多,但是路途遥远,小心堵车哦,小编只能在外备好月饼等各种好吃的,和家人视频"团圆"了.那选什么 ...

  5. Java基础练习——吃货联盟

    Java基础练习--吃货联盟 提示:使用选择.循环以及数组等基础进行编写 文章目录 Java基础练习--吃货联盟 前言 一.使用步骤 1.代码 2.编写思路 总结 前言 提示:这里可以添加本文要记录的 ...

  6. Java基础之吃货联盟订餐系统Version1.0

    注意:此项目为刚学完Java基础至数组的入门程序. 吃货联盟订餐系统Version1.0是在数组的基础上完成的一个小项目,由于只是运用数组实现了一些简单的功能, 所以我管它叫Version1.0,即1 ...

  7. [附源码]计算机毕业设计JAVA壹家吃货店网站

    [附源码]计算机毕业设计JAVA壹家吃货店网站 项目运行 环境配置: Jdk1.8 + Tomcat7.0 + Mysql + HBuilderX(Webstorm也行)+ Eclispe(Intel ...

  8. Java迷宫版 吃豆人(有音乐)

    Java吃豆人+迷宫 本来想着自己用Java编写的吃豆人的,但是如果自己画地图的话,那么多点不好弄,于是就想着让它自己随机生成地图,最后看起来还是可以的,而且我又在网上找了找音频,把声音配上去了, 还 ...

  9. python 嫦娥吃月饼

    import timedef consumer(name):print('我是%s,我这边准备吃月饼了'%name)while True:moon_cake = yieldtime.sleep(1)p ...

最新文章

  1. 专科电子信息工程不学c语言,高中数学物理都不是很好 想报电子信息工程专业(专科)能行么?...
  2. Linux下编译运行C程序
  3. syslog(),closelog()与openlog()--日志操作函数
  4. Java基础语法十二 泛型程序设计
  5. pandas parse_data出现异常时,自动跳过
  6. SAP CRM IPC and configuration page debugs
  7. add git 指定类型文件夹_UE4 使用git配合远程仓库
  8. 随想录(从技术到业务的转变)
  9. 点击IE窗口上方的“X关闭符号”,弹出提示窗口!
  10. Binary XML file line #23: Error inflating class android.widget.TextView
  11. Doing It in User Space
  12. 百度亮相NeurIPS 首届Expo:向世界科普了中国自动机器学习框架
  13. 对话机器人技术简介:问答系统、对话系统与聊天机器人
  14. 清明上河图轴卷图滑动
  15. 如何通过提问识人(行为面试法)
  16. Android点阵屏效果的控件
  17. 浅谈一下“敏捷开发”
  18. Keep熬过冬天,但互联网健身的生意依然不好做
  19. 【华为机试真题 Python实现】藏宝图
  20. django教程day06

热门文章

  1. python中的3d画图
  2. 群晖添加第三方套件源提示无效位置的解决方法(解决群晖CA根证书过期的问题)
  3. 【原创】RPA在保险行业的场景分享-中科云创CEO每日分享
  4. ntp时间同步会导致mysql关闭吗?_NTP时间同步
  5. 灌区智能测控一体化闸门系统解决方案
  6. 【iMessage苹果证书协议版本】软件安装APNSD-AppID配置中创建结构
  7. 无损音乐播放器Audirvana for mac
  8. OrientDB Python连接操作
  9. Linux用户需要知道的官方网站
  10. -在c++中是什么意思