【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 CME 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 CM 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

解题思路:

题目输入学生ID和成绩,按照题目要求的A>C>M>E的优先级次序,输出学生排名最好的成绩和科目编号。很自然地想到用结构来存储学生信息,然后分别进行排序,比较,将排名靠前的赋值给新的数组即可。然后用map构造一个<string,string>类型的容器,来更方便地存储学生ID对应的排名加成绩编号。

易错点:

1. 成绩排名是相同成绩的人名词一样,后一名的学生是前面学生的总人数+1。就是成绩为 99 98 98 37 名词为1 2 2 4 。

2. 排名的次序记得是A>C>M>E,最好写的时候就是按照这个顺序写,不然容易出错。

例如:

输入:

3 3 
310101 98 98 98  
310102 98 98 98   
310103 82 0 0  
310101 

310102  

310103  

输出:

1 A

1 A

3 A

代码:

#include<bits/stdc++.h>
using namespace std;
int N,M;
typedef struct Student{//定义一个结构来存储学生信息,例如学号,成绩,排名等等 
	string StudentID;
	int C;
	int M;
	int E;
	double A;
	int rank;
};

bool cmpA(Student a,Student b){//排序,按照A成绩的优先级进行排序,下面类推就行 
	return a.A>b.A;
}
bool cmpC(Student a,Student b){//C优先 
	return a.C>b.C;
}
bool cmpM(Student a,Student b){//M优先 
	return a.M>b.M;
}
bool cmpE(Student a,Student b){//E优先 
	return a.E>b.E;
}

void rr(Student a[],string c){
//因为排过序了,所以名次只要是i+1就行,如果成绩相同则和前一个人的名次一样 
	for(int i=0;i<N;i++){
		if(c=="A"){
			if(i>0&&a[i].A==a[i-1].A) a[i].rank=a[i-1].rank;
			else a[i].rank=i+1;
		}
		else if(c=="C"){
			if(i>0&&a[i].C==a[i-1].C) a[i].rank=a[i-1].rank;
			else a[i].rank=i+1;
		}
		else if(c=="M"){
			if(i>0&&a[i].M==a[i-1].M) a[i].rank=a[i-1].rank;
			else a[i].rank=i+1;
		}
		else if(c=="E"){
			if(i>0&&a[i].E==a[i-1].E) a[i].rank=a[i-1].rank;
			else a[i].rank=i+1;
		}
	}
}
int main(){
	cin>>N>>M;
	Student S[N];
	for(int i=0;i<N;i++){
		cin>>S[i].StudentID>>S[i].C>>S[i].M>>S[i].E;
		S[i].A=(S[i].C+S[i].E+S[i].M)/3;
	} 
	map<string, string> ss;//存储优先级和成绩代号 
	map<string,int> sr; //存储名次,便于判断 
	sort(S,S+N,cmpA);
	rr(S,"A");
	for(int i=0;i<N;i++){//按照A优先,将名词和A赋值 
		ss[S[i].StudentID]=to_string(S[i].rank)+" A";
		sr[S[i].StudentID]=S[i].rank;
	}
	sort(S,S+N,cmpC);
	rr(S,"C");
	for(int i=0;i<N;i++){//按照C优先,将名次在A前的赋值掉 
		if(S[i].rank<sr[S[i].StudentID]){
			ss[S[i].StudentID]=to_string(S[i].rank)+" C";
			sr[S[i].StudentID]=S[i].rank;
		}
	}
	sort(S,S+N,cmpM);
	rr(S,"M");
	for(int i=0;i<N;i++){
		if(S[i].rank<sr[S[i].StudentID]){
			ss[S[i].StudentID]=to_string(S[i].rank)+" M";
			sr[S[i].StudentID]=S[i].rank;
		}
	}
	sort(S,S+N,cmpE);
	rr(S,"E");
	for(int i=0;i<N;i++){
		if(S[i].rank<sr[S[i].StudentID]){
			ss[S[i].StudentID]=to_string(S[i].rank)+" E";
			sr[S[i].StudentID]=S[i].rank;
		}
	}
	for(int i=0;i<M;i++){
		string a;
		cin>>a;
		if(ss.count(a)!=NULL){
			string b = ss[a];
		cout<<ss[a]<<endl;
		}
		else{
			cout<<"N/A"<<endl;
		}
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值