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 Algebra), 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

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

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个考生的3门分数,平均分可以按照这三门算出来。然后分别对这四个分数从高到低排序,这样对每个考生来说有4个排名。k个查询,对于每一个学生id,输出当前id学生的最好的排名和它对应的分数,如果名次相同,按照A>C>M>E的顺序输出。如果当前id不存在,输出N/A
分析:
1.用结构体存储学生的id、四门成绩、四门排名、最好的排名的对应的科目下标
2.注意,排名并列应该1、1、3、4、5,而不是1、1、2、3、4,否则会有一个测试点不过
3.注意,平均分是四舍五入的,所以需要按照+0.5后取整,保证是四舍五入的(听说不四舍五入也能通过…)
4.存储的时候就按照ACME的顺序存储可以简化程序逻辑

实现代码如下:(简洁版)

#include<iostream>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;

struct student{
	string id;
	int score[4];
	int rank[4];
	int best_rank,best_id;
};

int flag = -1;

int cmp(student stu_1,student stu_2){
	return stu_1.score[flag] > stu_2.score[flag];
}

int main(){
	int N,M;
	cin>>N>>M;
	
	string id;
	int c,m,e,a;
	
	student stu[2000];
	
	for(int i=0;i<N;i++)
	{
		cin>>id>>c>>m>>e;
		a = (c+m+e)/3;
		stu[i].id = id;
		stu[i].score[0] = a;
		stu[i].score[1] = c;
		stu[i].score[2] = m;
		stu[i].score[3] = e;
	}
	
	string str[2000];
	for(int i = 0;i<M;i++)
		cin>>str[i];

	for(int j=0;j<=3;j++)
	{
		flag = j;
		sort(stu,stu+N,cmp);
		stu[0].rank[j] = 1;//第一个设为第一名 
		for(int k = 1;k<N;k++)
			if(stu[k].score[j] != stu[k-1].score[j])
				stu[k].rank[j] = k+1;
			else
				stu[k].rank[j] = stu[k-1].rank[j];//重复分数时名次保持一样 
	}
	
	for(int i=0;i<N;i++)
	{
		int best_rank = stu[i].rank[0];
		stu[i].best_rank = best_rank;//注意需要赋初始值 
		stu[i].best_id = 0;//注意需要赋初始值 
		
		for(int j=1;j<=3;j++)
		{
			if(best_rank > stu[i].rank[j]){
				best_rank = stu[i].rank[j];
				stu[i].best_rank = best_rank;
				stu[i].best_id = j; 
			}	
		}
	}
	
	char ch[] = {'A','C','M','E'};//字符数组打印等级要方便些 
	for(int i=0;i<M;i++)
	{
		int k = 0;
		
		for(int j=0;j<N;j++)
		{
			if( stu[j].id == str[i] ){
				cout<<stu[j].best_rank<<" "<<ch[ stu[j].best_id ]<<endl;	
				k = 1;
				break;
			}	
		}
		if(k == 0)//当没有这个学号时 
			cout<<"N/A"<<endl;	
	}
	
	return 0;
} 

代码实现方法(二):

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<math.h>
using namespace std;

struct student{
	string id;
	int c,m,e,a;
};

int sort_c(student stu_1,student stu_2){
	return stu_1.c > stu_2.c;
}

int sort_m(student stu_1,student stu_2){
	return stu_1.m > stu_2.m;
}

int sort_e(student stu_1,student stu_2){
	return stu_1.e > stu_2.e;
}

int sort_a(student stu_1,student stu_2){
	return stu_1.a > stu_2.a;
}

int find_rank_c(vector<student> vec,string id){
	int cur = vec[0].c,cur_rank = 0;
	for(int i = 0;i<vec.size();i++)
	{
		if(cur > vec[i].c){
			cur = vec[i].c;
			cur_rank = i;
		}
			
		if( vec[i].id == id)//string类型,直接比较 
		{
			return cur_rank;
		}
	}
	return vec.size();//表示没有找到 
}

int find_rank_m(vector<student> vec,string id){
	int cur = vec[0].m,cur_rank = 0;
	for(int i = 0;i<vec.size();i++)
	{
		if(cur > vec[i].m){
			cur = vec[i].m;
			cur_rank = i;
		}
			
		if( vec[i].id == id)//string类型,直接比较 
		{
			return cur_rank;
		}
	}
	return vec.size();//表示没有找到 
}

int find_rank_e(vector<student> vec,string id){
	int cur = vec[0].e,cur_rank = 0;
	for(int i = 0;i<vec.size();i++)
	{
		if(cur > vec[i].e){
			cur = vec[i].e;
			cur_rank = i;
		}
			
		if( vec[i].id == id)//string类型,直接比较 
		{
			return cur_rank;
		}
	}
	return vec.size();//表示没有找到 
}

int find_rank_a(vector<student> vec,string id){
	int cur = vec[0].a,cur_rank = 0;
	for(int i = 0;i<vec.size();i++)
	{
		if(cur > vec[i].a){
			cur = vec[i].a;
			cur_rank = i;//注意这里是等于i 
		}
			
		if( vec[i].id == id)//string类型,直接比较 
		{
			return cur_rank;
		}
	}
	return vec.size();//表示没有找到 
}

int main(){
	int N,M;
	cin>>N>>M;
	
	string id;
	int c,m,e,a;
	vector<student> vec;
	for(int i=0;i<N;i++)
	{
		cin>>id;
		cin>>c>>m>>e;
		a = (c+m+e)/3;
		vec.push_back(student{id,c,m,e,int(a+0.5)});
	}
	
	string check_id[2000];
	for(int i=0;i<M;i++)
	{
		cin>>check_id[i];
	}
	
	int rank_c,rank_m,rank_e,rank_a;
	
	for(int i=0;i<M;i++)
	{
		sort(vec.begin(),vec.end(),sort_a);
		rank_a = find_rank_a(vec,check_id[i]);// find_rank_a要避免分数相同时排名一样 
		
		sort(vec.begin(),vec.end(),sort_c);
		rank_c = find_rank_c(vec,check_id[i]);
		
		sort(vec.begin(),vec.end(),sort_m);
		rank_m = find_rank_m(vec,check_id[i]);
		
		sort(vec.begin(),vec.end(),sort_e);
		rank_e = find_rank_e(vec,check_id[i]);
		
		if(rank_a == N)
			cout<<"N/A"<<endl;
		
		else if(rank_a <= rank_c && rank_a <= rank_m && rank_a <= rank_e) 
			cout<<rank_a+1<<" "<<"A"<<endl;
			
		else if(rank_c <= rank_m && rank_c <= rank_e && rank_c <= rank_a) 
			cout<<rank_c+1<<" "<<"C"<<endl;
			
		else if(rank_m <= rank_c && rank_m <= rank_e && rank_m <= rank_a) 
			cout<<rank_m+1<<" "<<"M"<<endl;
			
		else if(rank_e <= rank_c && rank_e <= rank_m && rank_e <= rank_a) 
			cout<<rank_e+1<<" "<<"E"<<endl;		
	}	
	
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_29762941/article/details/81072986