当前位置: 代码迷 >> 综合 >> PAT乙级——1085(多标准排序)java实现
  详细解决方案

PAT乙级——1085(多标准排序)java实现

热度:22   发布时间:2023-12-13 06:23:07.0

没有AC,19分,暂时没找到问题所在,又发现的请留言,我改正了也会更新


题目: PAT单位排行 (25 分)

每次 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.BufferedReader;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.TreeSet;public class Main {
    public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));int num = Integer.parseInt(in.readLine());String[] input = new String[num];School[] school = new School[10000];int count = 0;// 学校的个数Set <School> set =new TreeSet<>();for (int i = 0; i < 1000; i++)school[i] = new School();for (int i = 0; i < num; i++) {
    input[i] = in.readLine();}in.close();for (int i = 0; i < num; i++) {
    Boolean schoolhas = false;String[] value = input[i].split(" ");char rate = value[0].charAt(0);// 考试等级Loop: for (int j = 0; j < count; j++) {
    if (value[2].equalsIgnoreCase(school[j].Name)) {
    schoolhas = true;switch (rate) {
    case 'B':school[j].score += Integer.parseInt(value[1]) * 2 / 3;school[j].studentNum++;break Loop;case 'A':school[j].score += Integer.parseInt(value[1]);school[j].studentNum++;break Loop;default:school[j].score += Integer.parseInt(value[1]) * 3 / 2;school[j].studentNum++;break Loop;}}}if (!schoolhas) {
    school[count].Name = value[2].toLowerCase();switch (rate) {
    case 'B':school[count].score += Integer.parseInt(value[1]) * 2 / 3;school[count].studentNum++;school[count].a = rate;break;case 'A':school[count].score += Integer.parseInt(value[1]);school[count].studentNum++;school[count].a = rate;break;default:school[count].score += Integer.parseInt(value[1]) * 3 / 2;school[count].studentNum++;school[count].a = rate;break;}count++;}}//加入TreeSet并排序for (int i = 0; i < count; i++) {
    set.add(school[i]);}int order =1;int temp =0;int score =0;System.out.println(count);for (School school2 :set) {
    if(score==school2.score) System.out.print(temp);				else {
    System.out.print(order);temp=order;//保存上一位的排名}		System.out.println(" " + school2.Name + " " + school2.score + " " + school2.studentNum);score = school2.score;//保存上一个人的分数order++;//前面的人数 }}
}class School implements Comparable<School> {
    String Name;int score;int studentNum;char a;@Overridepublic int compareTo(School o) {
    //按照分数排序int result = this.score - o.score;if (result != 0)return -result;else {
    //按照学生个数排序result = this.studentNum - o.studentNum;if (result != 0)return result;else {
    //按字典码排序,既每个字符串依次按照ASCll表排序,小的在前,都相同就按照长度排result = this.Name.compareTo(o.Name);return result;}}}
}

在这里插入图片描述
后面感觉可能是由于每一次都是取int型导致score不对,改用double型,且先相加再取整。

//效果一样,还是19分,没找到症结所在
import java.io.BufferedReader;import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;public class Main {
    public static void main(String[] args) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));int num = Integer.parseInt(in.readLine());String[] input = new String[num];School[] school = new School[10000];int count = 0;// 学校的个数Set <School> set =new TreeSet<>();for (int i = 0; i < 1000; i++)school[i] = new School();for (int i = 0; i < num; i++) {
    input[i] = in.readLine();}in.close();for (int i = 0; i < num; i++) {
    Boolean schoolhas = false;String[] value = input[i].split(" ");char rate = value[0].charAt(0);// 考试等级Loop: for (int j = 0; j < count; j++) {
    if (value[2].equalsIgnoreCase(school[j].Name)) {
    schoolhas = true;switch (rate) {
    case 'B':school[j].bscore += Integer.parseInt(value[1]);school[j].studentNum++;break Loop;case 'A':school[j].ascore += Integer.parseInt(value[1]);school[j].studentNum++;break Loop;default:school[j].tscore += Integer.parseInt(value[1]);school[j].studentNum++;break Loop;}}}if (!schoolhas) {
    //加入新的学校,名字小拼school[count].Name = value[2].toLowerCase();switch (rate) {
    case 'B':school[count].bscore += Integer.parseInt(value[1]);school[count].studentNum++;break;case 'A':school[count].ascore += Integer.parseInt(value[1]);school[count].studentNum++;break;default:school[count].tscore += Integer.parseInt(value[1]);school[count].studentNum++;break;}count++;}}for (int i = 0; i < count; i++) {
    school[i].score = (int)(school[i].bscore/1.5+school[i].ascore+school[i].tscore*1.5);}for (int i = 0; i < count; i++) {
    set.add(school[i]);}//Arrays.sort(school,new );int order =1;int temp =0;int score =0;System.out.println(count);for (School school2 :set) {
    if(score==school2.score) //如果分数与前一个人相同,输出前一个编号,temp不变System.out.print(temp);					else {
    System.out.print(order);temp=order;//保存上一位的排名}				System.out.println(" " + school2.Name + " " + school2.score + " " + school2.studentNum);score = school2.score;//保存上一个人的分数order++;//前面的人数}}}class School implements Comparable<School> {
    String Name;double bscore;double ascore;double tscore;int score;int studentNum;@Overridepublic int compareTo(School o) {
    int result = this.score - o.score;if (result != 0)return -result;else {
    result = this.studentNum - o.studentNum;if (result != 0)return result;else {
    result = this.Name.compareTo(o.Name);return result;}}}
}
  相关解决方案