01 排序【PAT A1025】PAT Ranking

1 题目

在这里插入图片描述

2 代码

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

public class A1025 {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        ArrayList<PATStudent> totalStudent = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            int K = Integer.parseInt(br.readLine());
            ArrayList<PATStudent> localStudents = new ArrayList<>();
            for (int j = 0; j < K; j++) {
                String[] input = br.readLine().split(" ");
                localStudents.add(new PATStudent(input[0], Integer.parseInt(input[1]), (i + 1)));
            }

            Collections.sort(localStudents, new Comparator<PATStudent>() {
                @Override
                public int compare(PATStudent o1, PATStudent o2) {
                    return o2.getScore() - o1.getScore();
                }
            });

            for (int j = 0; j < localStudents.size(); j++) {
                if (j == 0)
                    localStudents.get(0).setLocal_rank((1));
                else {
                    if (localStudents.get(j).getScore() == localStudents.get(j - 1).getScore())
                        localStudents.get(j).setLocal_rank(localStudents.get(j - 1).getLocal_rank());
                    else
                        localStudents.get(j).setLocal_rank(j + 1);
                }
            }

            totalStudent.addAll(localStudents);
        }

        Collections.sort(totalStudent, new Comparator<PATStudent>() {
            @Override
            public int compare(PATStudent o1, PATStudent o2) {
                if (o2.getScore() != o1.getScore())
                    return o2.getScore() - o1.getScore();
                else
                    return o1.getRegistration_number().compareTo(o2.getRegistration_number());
            }
        });

        for (int i = 0; i < totalStudent.size(); i++) {
            if (i == 0)
                totalStudent.get(0).setFinal_rank(1);
            else {
                if (totalStudent.get(i).getScore() == totalStudent.get(i - 1).getScore())
                    totalStudent.get(i).setFinal_rank(totalStudent.get(i - 1).getFinal_rank());
                else
                    totalStudent.get(i).setFinal_rank(i + 1);
            }
        }

        System.out.println(totalStudent.size());
        for (PATStudent s : totalStudent) {
            System.out.println(s.getRegistration_number() + " " + s.getFinal_rank() + " " + s.getLocation_number() + " " + s.getLocal_rank());
        }
    }
}

class PATStudent {
    private String registration_number;
    private int score;
    private int final_rank;
    private int location_number;
    private int local_rank;

    public PATStudent(String registration_number, int score, int location_number) {
        this.registration_number = registration_number;
        this.score = score;
        this.location_number = location_number;
    }

    public String getRegistration_number() {
        return registration_number;
    }

    public void setRegistration_number(String registration_number) {
        this.registration_number = registration_number;
    }

    public int getFinal_rank() {
        return final_rank;
    }

    public void setFinal_rank(int final_rank) {
        this.final_rank = final_rank;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getLocation_number() {
        return location_number;
    }

    public void setLocation_number(int location_number) {
        this.location_number = location_number;
    }

    public int getLocal_rank() {
        return local_rank;
    }

    public void setLocal_rank(int local_rank) {
        this.local_rank = local_rank;
    }
}

在这里插入图片描述

3 要点

(1)思路:遍历输入N个地区的考生信息,每个地区的考生信息分别用List记录其id、score、地区编号,并对List排序,最后保存到TotalList中去。最后对TotalList进行排序即可,注意同分数的考生排名相同,并且按字符串升序排列,按规则输出即可。
(2)用Java还是超时了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是聪聪黄吖

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

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

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

打赏作者

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

抵扣说明:

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

余额充值