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


    时间限制: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  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
    ", s[i].num);
            for (j=0; j<4; j++)
                printf("%3d ", s[i].score[j]);
            printf("
    ");
            for (j=0; j<4; j++)
                printf("%3d ", s[i].rank[j]);
            printf("
    ");
        }
    }
     
    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
    ");
                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
    ", p->rank[bestMethod], tmp[bestMethod]);
                }
            }
        }   
                 
        return 0;
    } 
    /**************************************************************
        Problem: 1324
        User: liangrx06
        Language: C
        Result: Accepted
        Time:20 ms
        Memory:916 kb
    ****************************************************************/


    编程算法爱好者。
  • 相关阅读:
    解决ActiveX插件ZIndex属性无效问题
    XNA游戏开发之字符篇
    XNA游戏开发之滚动背景
    ExtJs之FormPanel篇
    独立Discuz头像编辑模块
    XNA游戏开发之2D游戏
    WPF系列之应用程序生命周期
    DiscuzX2.0在windows下的配置
    Silverlight之out of Browser模式
    使用VisualStudio2010连接CodePlex进行代码管理
  • 原文地址:https://www.cnblogs.com/liangrx06/p/5083803.html
Copyright © 2020-2023  润新知