PTA-1012——The Best Rank
Posted orangecyh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA-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 (≤), 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
题目分析:
模拟题
代码:
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int N,M; 5 float grade[2001][4]; //记录所有人的成绩 6 string id[2001]; //记录所有人的id 7 int ran; //最高排名 8 int subject; //最高排名的科目编号 9 10 //输入id,获得该id对应人的序号,若查无此人则返回-1 11 int getNum(string id1){ 12 int i; 13 for(i=0;i<N;i++){ 14 if(id1==id[i]){ 15 return i; 16 } 17 } 18 if(i==N){ 19 return -1; 20 } 21 } 22 23 //获得排名 24 void getRank(int n){ 25 int ranklist[4]={1,1,1,1}; //该学生四个成绩的排名,以A/C/M/E的顺序对应 26 for(int i=0;i<N;i++){ 27 if(grade[i][0]>grade[n][0]){ 28 ranklist[1]++; 29 } 30 if(grade[i][1]>grade[n][1]){ 31 ranklist[2]++; 32 } 33 if(grade[i][2]>grade[n][2]){ 34 ranklist[3]++; 35 } 36 if(grade[i][3]>grade[n][3]){ 37 ranklist[0]++; 38 } 39 } 40 ran=2001; 41 subject=-1; 42 for(int i=0;i<4;i++){ 43 if(ranklist[i]<ran){ 44 ran=ranklist[i]; 45 subject=i; 46 } 47 } 48 cout<<ran<<" "; 49 switch(subject){ 50 case 0:cout<<"A"<<endl;break; 51 case 1:cout<<"C"<<endl;break; 52 case 2:cout<<"M"<<endl;break; 53 case 3:cout<<"E"<<endl;break; 54 } 55 } 56 57 int main(){ 58 cin>>N>>M; 59 for(int i=0;i<N;i++){ 60 cin>>id[i]; 61 grade[i][3]=0.0; 62 for(int j=0;j<=2;j++){ 63 cin>>grade[i][j]; 64 grade[i][3]+=grade[i][j]; 65 } 66 grade[i][3]/=3; 67 } 68 for(int i=0;i<M;i++){ 69 string id1; 70 cin>>id1; 71 int num=getNum(id1); 72 if(num==-1){ 73 cout<<"N/A"<<endl; 74 }else{ 75 getRank(num); 76 } 77 } 78 return 0; 79 }
以上是关于PTA-1012——The Best Rank的主要内容,如果未能解决你的问题,请参考以下文章