PAT A1012. The Best Rank (25)

1012 The Best Rank (25 分)
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:
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 C
1 M
1 E
1 A
3 A
N/A

题意:

有N个考生,考三门课,分数分别为C,M,E,平均分数A由这三门成绩求平均得到,现在分别按这四个成绩对考生从高到低排名,因此对于每个考生就会有四个排名且每个分数都会有一个排名。接下来会有M个查询,每个查询输入一个考生的id,输出这个考生四个排名中最高的排名其对应的成绩代码(A,C,M,E),如果对不同课程有相同排名,则按优先级A>C>M>E来输出,若查询的考生id不存在则输出N/A。

思路:

定义people型结构体存储每个学生的信息,包括id和四种类型成绩,stu数组存放学生们的信息。char类型数组存放各类科目的名称ACME。int型二维数组Rank存放以id为第一下标,科目类别为第二下标的排名。 kind表示当前的科目类别。cmp函数为排序依据函数。
第一个for循环将输入接收到stu数组中,第二个for循环外层循环将各类别成绩排序,并将分数最高的设为1。内层循环处理剩下的学生,若与前一位学生相同则排名相同,否则设置正确的排名。第三个for循环用来输出排序查询结果,若Rank[find][0]==0即总分未计算则说明该id不存在,否则通过内层for循环找出rank值最小的科目,

参考代码:

#include <cstdio>
#include <algorithm>
using namespace std;
struct people{
	int id;
	int grade[4];//存放四个分数; 
}stu[2010];
char course[4]={'A','C','M','E'};
int Rank[10000000][4]={0};//rank[id][0]-rank[id][4]为四门课对应的排名;
int kind;//按哪一科进行排序; 
bool cmp(people a,people b){
	return a.grade[kind]>b.grade[kind]; 
} 
int main(){
	int N,M;
	scanf("%d %d",&N,&M);
	for(int i=0;i<N;i++){//录入分数; 
		scanf("%d %d %d %d",&stu[i].id,&stu[i].grade[1],&stu[i].grade[2],&stu[i].grade[3]);
		stu[i].grade[0]=stu[i].grade[1]+stu[i].grade[2]+stu[i].grade[3];//实际上不用取平均,算总分即可。 
	}
	for(kind=0;kind<4;kind++){//四门依次排序;
		sort(stu,stu+N,cmp);//排序; 
		Rank[stu[0].id][kind]=1;//排序完将分数最高的(stu[0]为第1)设为第一名; 
		for(int i=1;i<N;i++){//剩下的学生; 
			if(stu[i].grade[kind]==stu[i-1].grade[kind]){//若与前一位学生分数相同 
				Rank[stu[i].id][kind]=Rank[stu[i-1].id][kind];//则排名相同; 
			}
			else Rank[stu[i].id][kind]=i+1;//否则为其设置正确的排名;正确的含义需要理解。
		} 
	}
	int find;
	for(int i=0;i<M;i++){
		scanf("%d",&find);
		if(Rank[find][0]==0) printf("N/A\n");//id不存在则输出N/A;
		else {
			int k=0;//选出rank[find][0-3]中最小的,rank值越小,排名越高; 
			for(int j=0;j<4;j++){
				if(Rank[find][j]<Rank[find][k]){//小于号保证k值尽量小,即保证了优先级。
					k=j;
				} 
			}
			printf("%d %c\n",Rank[find][k],course[k]);
		} 
	} 
	return 0;
} 

点评:

①此题属于排序型,题意较繁琐,耐下心来理顺逻辑即可。
②需注意几点:
确定当前学生某科成绩排名时,要看是否与他前一名同学分数相同,相同则排名与上一位相同,不同则正确排名应是他的编号+1。
在最后选取排名最高的科目类别时,if(Rank[find][j]<Rank[find][k]) 小于号保证k值尽量小,即保证了优先级。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值