1012. The Best Rank (25)
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 Input5 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 999999Sample Output
1 C 1 M 1 E 1 A 3 A N/A
应该用sort的..用qsort不仅麻烦还慢。
调用qsort把每个人的A C M E按照降序排列
注意若出现(90 90 81)的情况,则排名为(1 1 3)
#include <stdio.h> #include <string.h> #include <algorithm> #define MAX 2000 #define DIG 7 struct student { char id[DIG]; int c, m, e, a; }; student record[MAX]; struct C { char id[DIG]; int c; }; struct M { char id[DIG]; int m; }; struct E { char id[DIG]; int e; }; struct A { char id[DIG]; int a; }; A student_a[MAX]; E student_e[MAX]; M student_m[MAX]; C student_c[MAX]; int cmpA(const void *a, const void *b); int cmpC(const void *a, const void *b); int cmpM(const void *a, const void *b); int cmpE(const void *a, const void *b); int main() { int N, M; scanf("%d %d", &N, &M); for (int i = 0; i < N; i++) { scanf("%s %d %d %d", &record[i].id, &record[i].c, &record[i].m, &record[i].e); record[i].a = (int)((record[i].c + record[i].m + record[i].e) / 3.0 + 0.5); strcpy(student_a[i].id, record[i].id); strcpy(student_c[i].id, record[i].id); strcpy(student_e[i].id, record[i].id); strcpy(student_m[i].id, record[i].id); student_a[i].a = record[i].a; student_c[i].c = record[i].c; student_e[i].e = record[i].e; student_m[i].m = record[i].m; } qsort(student_c, N, sizeof(struct C), cmpC); qsort(student_m, N, sizeof(struct M), cmpM); qsort(student_e, N, sizeof(struct E), cmpE); qsort(student_a, N, sizeof(struct A), cmpA); for (int i = 0; i < M; i++) { char a[DIG]; bool flag = false; scanf("%s", &a); int rankC, rankM, rankE, rankA; for (int i = 0; i < N; i++) { if (strcmp(a, student_c[i].id) == 0) { flag = true; rankC = i + 1; for (int j = i - 1; j > -1; j--) { if (student_c[j].c == student_c[i].c) rankC--; else break; } break; } } if (flag == false) { printf("N/A\n"); } else { for (int i = 0; i < N; i++) { if (strcmp(a, student_m[i].id) == 0) { rankM = i + 1; for (int j = i - 1; j > -1; j--) { if (student_m[j].m == student_m[i].m) rankM--; else break; } break; } } for (int i = 0; i < N; i++) { if (strcmp(a, student_e[i].id) == 0) { rankE = i + 1; for (int j = i - 1; j > -1; j--) { if (student_e[j].e == student_e[i].e) rankE--; else break; } break; } } for (int i = 0; i < N; i++) { if (strcmp(a, student_a[i].id) == 0) { rankA = i + 1; for (int j = i - 1; j > -1; j--) { if (student_a[j].a == student_a[i].a) rankA--; else break; } break; } } if ((rankE < rankA) && (rankE < rankC) && (rankE < rankM)) { printf("%d E\n", rankE); } else if ((rankM < rankC) && (rankM < rankA)) { printf("%d M\n", rankM); } else if (rankC < rankA) { printf("%d C\n", rankC); } else { printf("%d A\n", rankA); } } } return 0; } int cmpA(const void *a, const void *b) { return (*(A*)b).a - (*(A*)a).a; } int cmpC(const void *a, const void *b) { return (*(C*)b).c - (*(C*)a).c; } int cmpM(const void *a, const void *b) { return (*(M*)b).m - (*(M*)a).m; } int cmpE(const void *a, const void *b) { return (*(E*)b).e - (*(E*)a).e; }