-
题目描述:
-
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 <stdlib.h>
#define N 2000
typedef struct node {
int num;
int score[4];
int rank[4];
} Stu;
int cmpNum(const void *a, const void *b)
{
return ((Stu *)a)->num - ((Stu *)b)->num;
}
int cmp0(const void *a, const void *b)
{
return ((Stu *)b)->score[0] - ((Stu *)a)->score[0];
}
int cmp1(const void *a, const void *b)
{
return ((Stu *)b)->score[1] - ((Stu *)a)->score[1];
}
int cmp2(const void *a, const void *b)
{
return ((Stu *)b)->score[2] - ((Stu *)a)->score[2];
}
int cmp3(const void *a, const void *b)
{
return ((Stu *)b)->score[3] - ((Stu *)a)->score[3];
}
void printStu(Stu *s, int n)
{
int i, j;
for (i=0; i<n; i++)
{
printf("%d\n", s[i].num);
for (j=0; j<4; j++)
printf("%3d ", s[i].score[j]);
printf("\n");
for (j=0; j<4; j++)
printf("%3d ", s[i].rank[j]);
printf("\n");
}
}
int main(void)
{
int n, m, i, j;
Stu s[N];
int check;
typedef int (*CMP)(const void *, const void *);
CMP cmp[4];
cmp[0] = cmp0;
cmp[1] = cmp1;
cmp[2] = cmp2;
cmp[3] = cmp3;
while (scanf("%d%d", &n, &m) != EOF)
{
for(i=0; i<n; i++)
{
scanf("%d%d%d%d", &s[i].num, &s[i].score[1], &s[i].score[2], &s[i].score[3]
);
s[i].score[0] = (s[i].score[1] + s[i].score[2] + s[i].score[3])/3;
}
for(j=0; j<4; j++)
{
qsort(s, n, sizeof(s[0]), cmp[j]);
for (i=0; i<n; i++)
{
int r = i-1;
while (r >= 0 && s[r].score[j] == s[i].score[j])
r --;
r += 2;
s[i].rank[j] = r;
}
}
qsort(s, n, sizeof(s[0]), cmpNum);
//printStu(s, n);
for (i=0; i<m; i++)
{
Stu current;
scanf("%d", &check);
current.num = check;
Stu *p = (Stu *)bsearch(¤t, s, n, sizeof(s[0]), cmpNum);
if (p == NULL)
printf("N/A\n");
else
{
int bestMethod = 0;
for (j=1; j<4; j++)
{
if (p->rank[j] < p->rank[bestMethod])
bestMethod = j;
}
char tmp[5] = "ACME";
printf("%d %c\n", p->rank[bestMethod], tmp[bestMethod]);
}
}
}
return 0;
}
/**************************************************************
Problem: 1324
User: liangrx06
Language: C
Result: Accepted
Time:20 ms
Memory:916 kb
****************************************************************/