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语言(C)、数学(M)和英语(E)课程上的成绩,要求你对他们的这几门课程以及课程平均分(A)进行排名,输出题目中要求的M名学生在其课程中(包括了C、M、E、A)最好的排名以及该最好的排名对应的课程。若有多门课程排名一致且均为其最好的排名,那么就按照A>C>M>E的优先级输出课程。
注意平均分需要四舍五入取整(这里使用了round()函数);若每门课程排名过程中,有学生的该门课程分数一致,那么他们的排名应当是相同的。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
struct Student{
int id;
int score[4];
};
int n,m;
vector<Student> students;
int way = -1;
int ans[1000000][4];
char courses[4] = {'A','C','M','E'};
bool visited[1000000] = {false};//标记学生是否存在
bool compare(Student a,Student b){
return a.score[way] > b.score[way];
}
int main(){
//思路
//1.用Student结构体数组存储每个学生的ID和对应课程的分数
//2.把每门课程都排一次序
//3.每排完一次序,就用二维数组(数组下标为学生id)记录下某学生某门课程的排名,因为课程等级有优先级,当学生有两门或以上课程排名相同时,则按照优先级输出某课程排名,因此可按照优先级存储课程等级(如[i][0]代表A,[i][1]代表C)
//4.输出学生最好的排名及其科目,若学生ID不存在则输出"N/A"
scanf("%d%d",&n,&m);
for(int i = 0 ; i < n ; i ++){
Student s;
scanf("%d%d%d%d",&s.id,&s.score[1],&s.score[2],&s.score[3]);
s.score[0] = round((s.score[1]+s.score[2]+s.score[3])/3.0);//注意四舍五入
students.push_back(s);
visited[s.id] = true;
}
for(int i = 0 ; i < 4 ; i ++){
//0是排A的序,1是排C,2是排M,3是排E
way = i;
sort(students.begin(),students.end(),compare);
ans[students[0].id][i] = 1;
for(int j = 1 ; j < students.size() ; j++){
if(students[j].score[i] == students[j-1].score[i])
ans[students[j].id][i] = ans[students[j-1].id][i];//考虑分数相同排名相等的情况
else
ans[students[j].id][i] = j+1;
}
}
for(int i = 1 ; i <= m ; i ++){
int id;
scanf("%d",&id);
if(visited[id] == false)
printf("N/A\n");
else
{
int min = 2000000;
int course = -1;
for(int j = 0 ; j < 4 ; j ++){
if(ans[id][j] < min){
min = ans[id][j];
course = j;
}
}
cout<<min<<" "<<courses[course]<<endl;
}
}
return 0;
}