【天梯赛—不想坑队友系列】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;
        }

        @Override
        public 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;
        }

        // 此类考生按德才总分从高到低排序  (降序)
        // 当某类考生中有多人总分相同时,按其德分降序排列;
        // 若德分也相同,则按准考证号的升序输出
        @Override
        public 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]);//no
            int b = Integer.parseInt(split[1]);//de_core
            int c = Integer.parseInt(split[2]);//cai_core
            if (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();
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DaoJis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值