原题链接
原题目:
1012. The Best Rank (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
本题主要是考察qsort函数及其cmp()的使用或者使用sort函数及其cmp(),两者的格式不能弄错,注意区分
其格式分析详见:
qsort及sort
同时,这里使用了结构体,要对结构体排序使用qsort函数的话其cmp函数结构需要一些改变:先将const void *类型的指针转化为指向结构体类型的指针,然后对其进行排序。
有位仁兄的对各种类型的cmp及qsort函数总结比较到位,链接如下:
各种类型的qsort
一下是我写的C语言代码,如有不足欢迎指正:
/*本题主要考察qsort函数与cmp函数的运用,同时要区分sort函数的cmp与qsort函数的cmp
详见笔记
*/
#include <stdio.h>
#include <stdlib.h>
struct Student{ //Student相当于为类型
int id;
int grade[4];
}Stu[2010]; //Stu【i】相当于是变量
char course[4]={'A','C','M','E'};
int Rank[1000000][4]={0};
int now;
int cmp(const void *A,const void *B) //const void *指定义了一个指向不可改变的常量值的指针,但是指针的指向可以改变
{
struct Student P=*(struct Student *)A;//使用结构体进行cmp排序
struct Student Q=*(struct Student *)B;//要先将类型转化为结构体
return Q.grade[now]-P.grade[now]; //从大到小排序
}
int main()
{
int N,M,i,j;
scanf("%d%d",&N,&M);
//grade【0】~【3】分别代表A,C,M,E
for(i=0;i<N;i++)
{
scanf("%d%d%d%d",&Stu[i].id,&Stu[i].grade[1],&Stu[i].grade[2],&Stu[i].grade[3]);
Stu[i].grade[0]=Stu[i].grade[1]+Stu[i].grade[2]+Stu[i].grade[3];
//由于题目中只要求比较A的大小,且未说明A的取值是如何取整的,便可以不需要求出平均数,只需要求出总数比较大小就行
}
for(now=0;now<4;now++) //now是全局变量
{
qsort(Stu,N,sizeof(struct Student),cmp); //类型不能写int,而是结构体的存储类型
Rank[Stu[0].id][now]=1; //第一个学生的排名为1
for(i=1;i<N;i++) //之后的学生排名要么和前面相同,要么为自己的序号加一(即前面有多少学生)
{
if(Stu[i].grade[now]==Stu[i-1].grade[now])
Rank[Stu[i].id][now]=Rank[Stu[i-1].id][now];
else Rank[Stu[i].id][now]=i+1;
}
}
int Query;//查询考生的ID
for(i=0;i<M;i++)
{
scanf("%d",&Query);
if(Rank[Query][0]==0)
printf("N/A\n");
else {
int k=0;
for(j=0;j<4;j++)
{
if(Rank[Query][j]<Rank[Query][k])
k=j;
}
printf("%d %c\n",Rank[Query][k],course[k]);
}
}
return 0;
}