当前位置: 首页 > news >正文

【天梯赛—不想坑队友系列】L2-003 月饼(java)

目录

第一题: L2-003 月饼

输入格式:

输出格式:

输入样例:

输出样例:

题目分析

题目代码

 第二题:德才论  

输入格式:

输出格式:

输入样例:

输出样例:

题目分析

思路:

题目代码


第一题: L2-003 月饼

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

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

代码长度限制

16 KB

时间限制

150 ms

内存限制

64 MB

题目分析

  •  一个元素有多个属性就想到构造类

  • 构造类用来存相应的属性可以重新定义一下排序的方法即compareTo()方法                    

题目代码

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;public class 月饼 {static PrintWriter out = new PrintWriter(System.out);static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));static StreamTokenizer in = new StreamTokenizer(ins);static class YueBing implements Comparable<YueBing> {double kucun;double sum_price;double one_price;public YueBing(double kucun, double sum_price) {this.kucun = kucun;this.sum_price = sum_price;this.one_price = sum_price / kucun;}@Overridepublic int compareTo(YueBing o) {if (o.one_price - this.one_price > 0)return 1; //降序排列else return -1;}}public static void main(String[] args) throws IOException {in.nextToken();int n = (int) in.nval;in.nextToken();int xuqiu = (int) in.nval;String[] s1 = ins.readLine().split(" ");String[] s2 = ins.readLine().split(" ");double res = 0;ArrayList<YueBing> list = new ArrayList<>();for (int i = 0; i < n; i++) {list.add(new YueBing(Double.parseDouble(s1[i]), Double.parseDouble(s2[i])));}Collections.sort(list);for (int i = 0; i < n; i++) {if (list.get(i).kucun >= xuqiu) {res += xuqiu * list.get(i).one_price;break;} else {res += list.get(i).sum_price;xuqiu -= (int)list.get(i).kucun ;}}System.out.printf("%.2f",res);}
}

 第二题:德才论  

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤105),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到优先录取线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

题目分析

思路:

  • 构造类存储属性

  • 将不同类别进行区分存储再排序

题目代码

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;public class 德才论 {static PrintWriter out = new PrintWriter(System.out);static BufferedReader ins = new BufferedReader(new InputStreamReader(System.in));static StreamTokenizer in = new StreamTokenizer(ins);static class Person implements Comparable<Person> {int no;int de_core;int cai_core;int sum_core;public Person(int no, int de_core, int cai_core) {this.no = no;this.de_core = de_core;this.cai_core = cai_core;this.sum_core = de_core + cai_core;}// 此类考生按德才总分从高到低排序  (降序)// 当某类考生中有多人总分相同时,按其德分降序排列;// 若德分也相同,则按准考证号的升序输出@Overridepublic int compareTo(Person o) {if (o.sum_core != this.sum_core) {return o.sum_core - this.sum_core;//降序排列:当左边的this.sum_core小于o.sum_core时返回1 交换俩者的位置;} else if (de_core != cai_core) {return o.de_core - this.de_core;//降序排列} else return this.no - o.no;//升序排列}}public static void main(String[] args) throws IOException {in.nextToken();int n = (int) in.nval;in.nextToken();int l = (int) in.nval;in.nextToken();int h = (int) in.nval;ArrayList<Person> list1 = new ArrayList();//存第一类人ArrayList<Person> list2 = new ArrayList();//存第二类人ArrayList<Person> list3 = new ArrayList();//存第三类人ArrayList<Person> list4 = new ArrayList();//存第四类人int res = 0;while (n-- > 0) {//输入数据String[] split = ins.readLine().split(" ");int a = Integer.parseInt(split[0]);//noint b = Integer.parseInt(split[1]);//de_coreint c = Integer.parseInt(split[2]);//cai_coreif (b >= l && c >= l) {res++;if (b >= h && c >= h) {list1.add(new Person(a, b, c));} else if (b >= h && c < h) {list2.add(new Person(a, b, c));} else if (b >= c && c < h && b < h) {list3.add(new Person(a, b, c));} else {list4.add(new Person(a, b, c));}}}Collections.sort(list1);//排序Collections.sort(list2);Collections.sort(list3);Collections.sort(list4);out.println(res);//输出for (int i = 0; i < list1.size(); i++) {out.println(list1.get(i).no + " " + list1.get(i).de_core + " " + list1.get(i).cai_core);}for (int i = 0; i < list2.size(); i++) {out.println(list2.get(i).no + " " + list2.get(i).de_core + " " + list2.get(i).cai_core);}for (int i = 0; i < list3.size(); i++) {out.println(list3.get(i).no + " " + list3.get(i).de_core + " " + list3.get(i).cai_core);}for (int i = 0; i < list4.size(); i++) {out.println(list4.get(i).no + " " + list4.get(i).de_core + " " + list4.get(i).cai_core);}out.flush();}
}

http://www.lryc.cn/news/59947.html

相关文章:

  • 电磁兼容(EMC)的标准与测试内容
  • 滑动平均算法
  • 个人职业发展
  • 剑指 Offer 51. 数组中的逆序对
  • 数字化转型迫在眉睫!药企如何应用AI技术加速创新?
  • 电脑显示屏是怎么显示出图像的?CPU与GPU又是什么关系?
  • 报名截至在即 | “泰迪杯”挑战赛最后一场赛前指导直播!
  • 经验分享:如何有效应对Facebook广告数据波动问题?
  • 【Python】逆向解析js代码
  • websorm启动vue项目修改内容后自动运行内存溢出
  • 第05章_数组
  • Spring Security --- 快速入门
  • 程序员挣够了钱,到中年失业真的很可怕吗?
  • 【Log and Dump Summary】
  • 软考证书找工作有用吗?软考找工作用处大吗
  • JavaWeb之谈论项目编码规范_Java版
  • Map排序
  • mycat读写分离
  • [Linux]环境变量
  • 次优二叉查找树(次优查找树)_递归和非递归实现_20230414
  • 贯穿设计模式第八话--设计原则总结篇
  • 地理信息系统(ArcGIS)在水文水资源、水环境中的实践技术应用及案例分析
  • 部分国产水文水动力模型介绍
  • HTTP请求
  • 网络威胁情报项目:为什么仍然很疯狂
  • Linux系统下使用shell“多线程执行命令”
  • HighTec编译器错误记录
  • 智慧校园大数据云平台(3)
  • 《花雕学AI》15:BingGPT桌面端——尝鲜体验ChatGPT4.0同源技术新Bing的最新成果
  • 反序列化漏洞及PHP魔法函数