PAT甲级-1012-The Best Rank(分数排序)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input 1:
5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999
Sample Output 1:
1 C
1 M
1 E
1 A
3 A
N/A

数据结构存储:

1、由于存在4个排位(三科排位+平均分排位),而排序中有相似的代码模板,如果写4个cmp,会存在冗余。————解决方案:用一维数组存储各科分数 其中score[0]存储平均分,每次调用cmp时,改变一下数组下标即可,实现了4个cmp共用一个cmp模板的功能
2、score【4】(各科分数),rank【4】(各科排位),c【4】(各科名称){‘A’,‘C’,‘M’,‘E’}一 一对应,使存储结果上具有一致性,其中用下标bestSbj标记最好排位的来源科目下标。
3、结构体数组下标和学生id之间通过一维数组exist[]建立联系,便于检索和定位。
注意:数组默认初始化0!!!

代码如下

#include<iostream>
#include<algorithm>
using namespace std;
struct student{
	int id;
	int score[4],rank[4];
	int bestSbj;
}stu[2005];
int index = -1,exist[10000000];
int cmp(student a,student b){
	return a.score[index] > b.score[index];
}
int main()
{
	int n,m;
	cin>>n>>m;
	for(int i = 0; i < n; i++){
		cin>>stu[i].id>>stu[i].score[1]>>stu[i].score[2]>>stu[i].score[3];
		stu[i].score[0]=(stu[i].score[1]+stu[i].score[2]+stu[i].score[3])/3;
	} 
	for(index = 0;index <= 3; index++){
		sort(stu, stu+n, cmp);
		stu[0].rank[index] = 1;
		for(int i = 1; i < n; i++){
			stu[i].rank[index] = i+1;
			//处理并列情况
			if(stu[i].score[index]==stu[i-1].score[index]){
				stu[i].rank[index] = stu[i-1].rank[index];
			} 
		}
	}
	for(int i = 0; i < n; i++){
		exist[stu[i].id] = i+1;
		int minRank = stu[i].rank[0];
		for(int j = 1; j <= 3; j++){
			if(stu[i].rank[j] < minRank) {
				minRank = stu[i].rank[j];
				stu[i].bestSbj = j;//记录最好的排位来自哪个科目 
			}
		}
	}
	char c[4] = {'A','C','M','E'};
	for(int i = 0; i < m; i++){
		int id;scanf("%d", &id);
		if(!exist[id]){
			cout<<"N/A"<<endl;
		}else{
			int tmp = exist[id]-1;
			cout<<stu[tmp].rank[stu[tmp].bestSbj]<<" "<<c[stu[tmp].bestSbj]<<endl;
		}
	}
	return 0;
}
发布了110 篇原创文章 · 获赞 746 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_42437577/article/details/104121344
今日推荐