题目
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个查询
输出该学生按什么成绩排序,排名最高,排名是多少。(可按c语言成绩、数学成绩、英语成绩、平均分数)
若没该学生,则输出N/A。
大坑
一开始想的很简单,结构体模拟,结果只对了两组数据。后来发现有个大坑。就是并列排名。
例如:1 1 2 2 5 这样,而不是简单的一个for循环。
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct stu
{
char str[8];
int C;
int M;
int E;
int sum;
int cc,aa,mm,ee;
int num;
char q;
};
int cmp1(stu a,stu b)
{
return a.C>b.C;
}
int cmp2(stu a,stu b)
{
return a.E>b.E;
}
int cmp3(stu a,stu b)
{
return a.M>b.M;
}
int cmp4(stu a,stu b)
{
return a.sum>b.sum;
}
stu hx[1000003];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
{
scanf("%s%d%d%d",hx[i].str,&hx[i].C,&hx[i].M,&hx[i].E);
hx[i].sum = hx[i].C + hx[i].M + hx[i].E;
}
sort(hx,hx+n,cmp4);
for(int i=0; i<n; i++)
{
if(i!=0 && hx[i].sum==hx[i-1].sum)
hx[i].aa=hx[i-1].aa;
else
hx[i].aa = i;
}
sort(hx,hx+n,cmp1);
for(int i=0; i<n; i++)
{
if(i!=0 && hx[i].C==hx[i-1].C)
hx[i].cc=hx[i-1].cc;
else
hx[i].cc = i;
}
sort(hx,hx+n,cmp3);
for(int i=0; i<n; i++)
{
if(i!=0 && hx[i].M==hx[i-1].M)
hx[i].mm=hx[i-1].mm;
else
hx[i].mm = i;
}
sort(hx,hx+n,cmp2);
for(int i=0; i<n; i++)
{
if(i!=0 && hx[i].E==hx[i-1].E)
hx[i].ee=hx[i-1].ee;
else
hx[i].ee = i;
}
for(int i=0;i<n;i++)
{
if(hx[i].aa<=hx[i].cc && hx[i].aa<=hx[i].ee &&hx[i].aa<=hx[i].mm)
{
hx[i].q='A';
hx[i].num=hx[i].aa;
}
else if(hx[i].cc<=hx[i].ee && hx[i].cc<hx[i].aa &&hx[i].cc<=hx[i].mm)
{
hx[i].q='C';
hx[i].num=hx[i].cc;
}
else if(hx[i].mm<hx[i].cc && hx[i].mm<=hx[i].ee &&hx[i].mm<hx[i].aa)
{
hx[i].q='M';
hx[i].num=hx[i].mm;
}
else if(hx[i].ee<hx[i].cc && hx[i].ee<hx[i].aa &&hx[i].ee<hx[i].mm)
{
hx[i].q='E';
hx[i].num=hx[i].ee;
}
}
char mz[10];
int flag;
for(int i=0; i<m; i++)
{
scanf("%s",mz);
flag=0;
for(int j=0; j<n; j++)
{
if(strcmp(mz,hx[j].str)==0)
{
printf("%d %c\n",hx[j].num+1,hx[j].q);
flag=1;
break;
}
}
if(!flag)
printf("N/A\n");
}
return 0;
}