01 排序【PAT A1055】The World‘s Richest

1 题目

在这里插入图片描述

2 代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class A1055 {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");

        int N = Integer.parseInt(input[0]);
        int K = Integer.parseInt(input[1]);

        ArrayList<Richest> totalRichest = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            String[] line = br.readLine().split(" ");
            totalRichest.add(new Richest(line[0], Integer.parseInt(line[1]), Integer.parseInt(line[2])));
        }

        for (int i = 0; i < K; i++) {
            String[] line = br.readLine().split(" ");
            int ageMin = Integer.parseInt(line[1]);
            int ageMax = Integer.parseInt(line[2]);

            ArrayList<Richest> richest = new ArrayList<>();
            for (Richest r : totalRichest) {
                if (r.getAge() >= ageMin && r.getAge() <= ageMax)
                    richest.add(r);
            }

            System.out.println("Case #" + (i + 1) + ":");
            if (richest.size() == 0) {
                System.out.println("None");
            } else {
                Collections.sort(richest, new Comparator<Richest>() {
                    @Override
                    public int compare(Richest o1, Richest o2) {
                        if (o1.getMoney() != o2.getMoney())
                            return o2.getMoney() - o1.getMoney();
                        else if (o1.getAge() != o2.getAge())
                            return o1.getAge() - o2.getAge();
                        else
                            return o1.getName().compareTo(o2.getName());
                    }
                });
                int num = Integer.parseInt(line[0]) > richest.size() ? richest.size() : Integer.parseInt(line[0]);
                for (int j = 0; j < num; j++) {
                    System.out.println(richest.get(j).getName() + " " + richest.get(j).getAge() + " " + richest.get(j).getMoney());
                }
            }

        }
    }
}

class Richest {
    private String name;
    private int age;
    private int money;

    public Richest(String name, int age, int money) {
        this.name = name;
        this.age = age;
        this.money = money;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
}

在这里插入图片描述

3 要点

(1)思路:首先将所有富人的信息保存到totalList中,然后按照查询的要求,对年龄进行筛选,满足条件的富人添加到一个List中。然后,如果List的长度为0,输出None;反之,按照worth、age、name依次对List进行排序,最后按要求输出即可。
(2)依旧存在运行超时的测试点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是聪聪黄吖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值