九度OJ 1324:The Best Rank(最优排名) (排序)

来源:互联网 发布:微信服务号域名 编辑:程序博客网 时间:2024/04/29 10:49

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:489

解决:126

题目描述:

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  A310101     98 85 88 90310102     70 95 88 84310103     82 87 94 88310104     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 6310101 98 85 88310102 70 95 88310103 82 87 94310104 91 91 91310105 85 90 90310101310102310103310104310105999999
样例输出:
1 C1 M1 E1 A3 AN/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****************************************************************/


0 0
原创粉丝点击