1085 PAT单位排行 (25 分) (JAVA解题)

64 篇文章 1 订阅

每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。本题就请你实现这个功能。

输入格式:
输入第一行给出一个正整数 N(≤10
​5
​​ ),即考生人数。随后 N 行,每行按下列格式给出一个考生的信息:

准考证号 得分 学校
其中准考证号是由 6 个字符组成的字符串,其首字母表示考试的级别:B代表乙级,A代表甲级,T代表顶级;得分是 [0, 100] 区间内的整数;学校是由不超过 6 个英文字母组成的单位码(大小写无关)。注意:题目保证每个考生的准考证号是不同的。

输出格式:
首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜:

排名 学校 加权总分 考生人数
其中排名是该单位的排名(从 1 开始);学校是全部按小写字母输出的单位码;加权总分定义为乙级总分/1.5 + 甲级总分 + 顶级总分*1.5的整数部分;考生人数是该属于单位的考生的总人数。

学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序输出。

输入样例:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

输出样例:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(br.readLine());
        PAT pat[] = new PAT[n];
        List<Information> list = new ArrayList<>();//用来存放要打印的信息
        boolean flag = false;
        for (int i = 0; i < n; i++) {
            String temp[] = br.readLine().split(" ");
            //存入每一个的对应的信息
            pat[i] = new PAT(temp[0].substring(0, 1), temp[0].substring(1, temp[0].length() - 1), Integer.parseInt(temp[1]), temp[2].toLowerCase());
            for (int j = 0; j < list.size(); j++) {
                Information temp1 = list.get(j);
                if(temp1.school.equals(pat[i].school)){//如果学校一样,就数量+1,成绩汇总
                    flag  = true;
                    temp1.number += 1;
                    if(pat[i].level.equals("A")) {
                        temp1.grade = temp1.grade + pat[i].grade;
                    }
                    if(pat[i].level.equals("B")) {
                        temp1.grade = (int) (temp1.grade + pat[i].grade / 1.5);
                    }
                    if(pat[i].level.equals("T")) {
                        temp1.grade = (int) (temp1.grade + pat[i].grade * 1.5);
                    }
                }
            }
            if(flag){
                flag = false;
            }else{//如果找不到一样的学校,就将新的学校添加到list中
                if(pat[i].level.equals("A")) {
                    list.add(new Information(pat[i].school,pat[i].grade));
                }
                if(pat[i].level.equals("B")) {
                    list.add(new Information(pat[i].school, (int) (pat[i].grade/1.5)));
                }
                if(pat[i].level.equals("T")) {
                    list.add(new Information(pat[i].school, (int) (pat[i].grade*1.5)));
                }
            }
        }
        Collections.sort(list);//排序
        System.out.println(list.size());
        int temp = 1;
        for (int i = 0; i < list.size(); i++) {
            if(i>0&&list.get(i).grade==list.get(i-1).grade){//用一个temp替换,如果成绩一样就保持和原来一样的排名
                System.out.printf("%d %s %d %d\n",i-temp+1,list.get(i).school,list.get(i).grade,list.get(i).number);
                temp++;
            }else{//如果不一样就输入i+1对应的排名,将temp置为1
                System.out.printf("%d %s %d %d\n",i+1,list.get(i).school,list.get(i).grade,list.get(i).number);
                temp = 1;
            }
        }
    }
}

class PAT{
    public String level;//代表等级
    public String code;//准考证
    public int grade;//成绩
    public String school;//学校

    public PAT(String level, String code, int grade, String school) {
        this.level = level;
        this.code = code;
        this.grade = grade;
        this.school = school;
    }
}

class Information implements Comparable<Information>{
    String school;//代表学校
    int grade;//代表总成绩
    int number = 1;//代表人数

    public Information(String school, int grade) {
        this.school = school;
        this.grade = grade;
    }
    public int compareTo(Information information){
        if(this.grade > information.grade){//成绩降序
            return -1;
        }else if(this.grade < information.grade){
            return 1;
        }else{
            if(this.number > information.number){//人数升序
                return 1;
            }else if(this.number < information.number){
                return -1;
            }else{
                return this.school.compareTo(information.school);//默认字典顺序
            }
        }
    }
}
//最后两个测试点超时

//最后两个测试点超时

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一般的一天

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

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

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

打赏作者

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

抵扣说明:

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

余额充值