美文网首页
【PAT-甲级-C++】1012. The Best Rank

【PAT-甲级-C++】1012. The Best Rank

作者: linghugoogle | 来源:发表于2018-01-27 19:49 被阅读14次

    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
    

    2、说明

    1)参考:https://www.cnblogs.com/8023spz/p/7666883.html
    2)考察知识:数据结构、Map
    3)坑,自己刚开始以为A > C > M > E代表排名冲突的时候按照这个优先级排序,在C的时候C > A > M > E,错的一塌糊涂。

    3、代码

    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<string>
    #define N 2018
    #define INF 6666
    using namespace std;
    int w;
    typedef struct Node {
        string name;
        int c, m, e, a;
        int bc, bm, be, ba;
    }node;
    bool compare(node n1, node n2) {
        if (w == 0) return n1.a > n2.a;
        else if (w == 1) return n1.c > n2.c;
        else if (w == 2) return n1.m > n2.m;
        else return n1.e > n2.e;
    }
    void print_best(node n1) {
        char best_class = 'A';
        int best_grade = n1.ba;
        if (n1.bc < best_grade) {
            best_class = 'C';best_grade = n1.bc;
        }
        if (n1.bm < best_grade) {
            best_class = 'M'; best_grade = n1.bm;
        }
        if (n1.be < best_grade) {
            best_class = 'E';best_grade = n1.be;
        }
    
        printf("%d %c\n", best_grade, best_class);
        return;
    }
    int main() {
        int n, m;               //<=2000
        char four_class[4] = { 'A','C','M','E' };
        node stu[N];
        int i, j;
        string str;
        map<string, int> match;
        //读入数据
        cin >> n >> m;
        for (i = 0;i < n;++i) {
            cin >> stu[i].name >> stu[i].c >> stu[i].m >> stu[i].e;
            stu[i].a= stu[i].c + stu[i].m + stu[i].e;
            
        }
        //排序
        //E
        w = 3;
        sort(stu, stu + n, compare);
        for (i = 0;i < n;++i) {
            if (i>0 && stu[i].e == stu[i - 1].e) stu[i].be = stu[i-1].be;
            else stu[i].be = i+1;
        }
        //M
        w = 2;
        sort(stu, stu + n, compare);
        for (i = 0;i < n;++i) {
            if (i>0 && stu[i].m == stu[i - 1].m) stu[i].bm = stu[i-1].bm;
            else stu[i].bm = i + 1;
        }
        //C
        w = 1;
        sort(stu, stu + n, compare);
        for (i = 0;i < n;++i) {
            if (i>0 && stu[i].c == stu[i - 1].c) stu[i].bc = stu[i-1].bc;
            else stu[i].bc = i + 1;
        }
        //A
        w = 0;
        sort(stu, stu + n, compare);
        for (i = 0;i < n;++i) {
            if (i>0 && stu[i].a == stu[i - 1].a) stu[i].ba = stu[i-1].ba;
            else stu[i].ba = i + 1;
            match[stu[i].name] = i + 1;
        }
    
        //匹配
        for (i = 0;i < m;++i) {
            cin >> str;
            if (match[str])
                print_best(stu[match[str]-1]);
            else printf("N/A\n");
        }
    
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:【PAT-甲级-C++】1012. The Best Rank

          本文链接:https://www.haomeiwen.com/subject/llkoaxtx.html