1. 云栖社区>
2. 博客列表>
3. 正文

# 九度题目1324：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.

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.

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".

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样例输出：
1 C
1 M
1 E
1 A
3 A
N/A

```/*二维数组的横纵向排序问题，用到了结构体，注意成绩相同

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int ID,C,M,E,A;//学生的编号，三科的成绩和平均成绩
int PC,PM,PE,PA;//这几科的排名
int con;//最高排名的成绩
char val;//最高排名的课的名字
}stu[2500];
int cmp1(node a,node b)//按C成绩排序
{
if(a.C!=b.C) return a.C>b.C;
}
int cmp2(node a,node b)//按M成绩排序
{
if(a.M!=b.M) return a.M>b.M;
}
int cmp3(node a,node b)//按E成绩排序
{
if(a.E!=b.E) return a.E>b.E;
}
int cmp4(node a,node b)//按平均成绩排序
{
if(a.A!=b.A) return a.A>b.A;
}
int min(int n,int m)
{return n<m?n:m;}
int main()
{
int i,j,n,m,x;
while(scanf("%d %d",&n,&m)!=EOF)
{
memset(stu,0,sizeof(stu));
for(i=0;i<n;i++)
{
scanf("%d %d %d %d",&stu[i].ID,&stu[i].C,&stu[i].M,&stu[i].E);
stu[i].A=(stu[i].C+stu[i].M+stu[i].E)/3;
}

//假设四个人的C成绩为 90 90 80 70 则排名为 1 1 3 4
//所以注意成绩一样的同学,排名是一样的（第三个人的排名是3而不是二）
sort(stu,stu+n,cmp1);
for(i=0;i<n;i++)
{
if(i==0)
stu[i].PC=i+1;

if(i!=0&&stu[i].C!=stu[i-1].C)
stu[i].PC=i+1;

if(i!=0&&stu[i].C==stu[i-1].C)
stu[i].PC=stu[i-1].PC;
}
sort(stu,stu+n,cmp2);
for(i=0;i<n;i++)
{
if(i==0)
stu[i].PM=i+1;

if(i!=0&&stu[i].M!=stu[i-1].M)
stu[i].PM=i+1;

if(i!=0&&stu[i].M==stu[i-1].M)
stu[i].PM=stu[i-1].PM;
}
sort(stu,stu+n,cmp3);
for(i=0;i<n;i++)
{
if(i==0)
stu[i].PE=i+1;

if(i!=0&&stu[i].E!=stu[i-1].E)
stu[i].PE=i+1;

if(i!=0&&stu[i].E==stu[i-1].E)
stu[i].PE=stu[i-1].PE;
}
sort(stu,stu+n,cmp4);
for(i=0;i<n;i++)
{
if(i==0)
stu[i].PA=i+1;

if(i!=0&&stu[i].A!=stu[i-1].A)
stu[i].PA=i+1;

if(i!=0&&stu[i].A==stu[i-1].A)
stu[i].PA=stu[i-1].PA;
}

for(i=0;i<n;i++)
{
stu[i].con=min(min(stu[i].PC,stu[i].PM),min(stu[i].PE,stu[i].PA));//得到最高排名
//得到最高排名的科目名称(相同的按优先级较高的为准)
if(stu[i].PA==stu[i].con) stu[i].val='A';
else
{
if(stu[i].PC==stu[i].con) stu[i].val='C';
else
{
if(stu[i].PM==stu[i].con) stu[i].val='M';
else
{
if(stu[i].PE==stu[i].con) stu[i].val='E';
}
}
}
}

int flag;
for(i=0;i<m;i++)
{
scanf("%d",&x);
flag=0;
for(j=0;j<n;j++)
{
if(x==stu[j].ID)
{
printf("%d %c\n",stu[j].con,stu[j].val);
flag=1;
}
if(flag==1)
break;
}
if(flag==0)
printf("N/A\n");
}
}
return 0;
}```

【云栖快讯】新年大招！云栖社区为在读大学生/研究生准备了一份学（huan）习（zhuang）攻略，发布博文即有机会赢得iPad mini 4等大奖，学习换装两不误！欢迎报名参与~  详情请点击

### 网友评论

696篇文章|20关注

RDS是一种稳定可靠、可弹性伸缩的在线数据库服务。支持MySQL、SQL Server、PostgreSQL、高... 更多>

2017云栖社区年度特辑