PAT(B) 1085 PAT单位排行(Java:20分)

68 篇文章 3 订阅
2 篇文章 0 订阅

题目链接:1085 PAT单位排行 (25 point(s))

题目描述

每次 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

分析?

最后两个测试点超时。如果有大佬做出来,希望能给我个提示。

定义一个类 School 通过输入保存本校的所有考生的顶级总分甲级总分乙级总分

School 类型的数组 school 保存所有学校的信息。

定义一个 Map<String, Integer> 保存学校的单位码下标,用于查找学校在数组中的位置,避免用遍历的方法查找。

然后将数组按照规则排好序,得到排名列表。依次输出即可。

注意:总评分数相同的学校排名相同,但是每个学校占一个名额。

Java 代码

第一次用英文注释,不过别担心,都非常简单,看不懂英文看代码总能看懂吧。有人说“代码是最好的注释”。还是有道理的。

/*******************************************************************************
Submit Time			Status	Score	Problem	Compiler		Run Time	User
8/29/2019, 21:14:17	PA		20		1085	Java (openjdk)	79 ms		wowpH
Case 4, 5: TLE
*******************************************************************************/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Main {
	public static void main(String[] args)	throws NumberFormatException,
											IOException {
		// 1、The input stream.
		InputStreamReader in = new InputStreamReader(System.in);
		BufferedReader input = new BufferedReader(in);

		// 2、Input the number of student.
		String str = input.readLine();
		int studentNumber = Integer.parseInt(str);// The number of student.

		// 3、Key is the unit code and value is the school subscript.
		Map<String, Integer> map = new HashMap<String, Integer>();

		School[] school = new School[studentNumber];
		int schoolNumber = 0;// The number of school.

		// 4、Enter information of studentNumber students.
		for (int i = 0; i < studentNumber; ++i) {
			str = input.readLine();
			String[] arr = str.split(" ");

			// 5、The test level, test score and unit code.
			char level = arr[0].charAt(0);
			int score = Integer.parseInt(arr[1]);
			String unitCode = arr[2].toLowerCase();

			// 6、Save the information.
			if (map.containsKey(unitCode)) {// Schools already exist.
				int index = map.get(unitCode);
				school[index].addScore(score, level);
				school[index].addOneStudent();// Student population plus 1
			} else {
				school[schoolNumber] = new School();
				school[schoolNumber].setUnitCode(unitCode);
				school[schoolNumber].addScore(score, level);
				school[schoolNumber].addOneStudent();

				// 7、Add the unit code and subscript to the map.
				map.put(unitCode, schoolNumber);
				++schoolNumber;
			}
		}

		// 8、Calculate the total score.
		for (int i = 0; i < schoolNumber; ++i) {
			double topScore = school[i].getTopScore() * 1.5;
			double bScore = school[i].getbScore() * 2.0 / 3;
			double totalScore = topScore + school[i].getaScore() + bScore;
			school[i].setTotalScore((int) totalScore);
		}

		// 9、Get the rank list.
		Arrays.sort(school, 0, schoolNumber);

		// 10、Output the rank list.
		int rank = 1, preScore = -1;
		System.out.println(schoolNumber);
		for (int i = 0; i < schoolNumber; ++i) {
			if (preScore != school[i].getTotalScore()) {
				rank = i + 1;
				preScore = school[i].getTotalScore();
			}
			System.out.print(rank + " ");
			System.out.print(school[i].getUnitCode() + " ");
			System.out.print(school[i].getTotalScore() + " ");
			System.out.println(school[i].getStudentNumber());
		}
	}
}

class School implements Comparable<School> {
	private String unitCode;// The unit code.
	private int topScore = 0;// The top score.
	private int aScore = 0;// Class a total score.
	private int bScore = 0;// Class b total score.
	private int totalScore = 0;// The weighted total score.
	private int studentNumber = 0;// The number of students.

	public String getUnitCode() {
		return unitCode;
	}

	public void setUnitCode(String unitCode) {
		this.unitCode = unitCode;
	}

	public int getTopScore() {
		return topScore;
	}

	public int getaScore() {
		return aScore;
	}

	public int getbScore() {
		return bScore;
	}

	public int getTotalScore() {
		return totalScore;
	}

	public void setTotalScore(int totalScore) {
		this.totalScore = totalScore;
	}

	public int getStudentNumber() {
		return studentNumber;
	}

	public void addOneStudent() {
		++this.studentNumber;
	}

	public void addScore(	int score,
							char level) {
		switch (level) {
		case 'T':
			topScore += score;
			break;
		case 'A':
			aScore += score;
			break;
		case 'B':
			bScore += score;
			break;
		default:
		}
	}

	@Override
	public int compareTo(School school) {
		// Descending order of total scores.
		if (totalScore != school.getTotalScore()) {
			return school.getTotalScore() - totalScore;
		}
		// Student Numbers in ascending order.
		if (studentNumber != school.getStudentNumber()) {
			return studentNumber - school.getStudentNumber();
		}
		// Ascending sorting of unit code.
		return unitCode.compareTo(school.getUnitCode());
	}
}

提交结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值