zoukankan      html  css  js  c++  java
  • PAT 甲级 1012 The Best Rank

    https://pintia.cn/problem-sets/994805342720868352/problems/994805502658068480

    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 CME 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 Specification:

    Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤), 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 CM and E. Then there are M lines, each containing a student ID.

    Output Specification:

    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

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    int n, m;
    map<int, int> mp;
    
    struct Node{
        int A, C, M, E;
        int na, nc, nm, ne;
        int id;
    }node[2020];
    
    bool cmpA(const Node& a, const Node& b) {
        return a.A > b.A;
    }
    
    bool cmpC(const Node& a, const Node& b) {
        return a.C > b.C;
    }
    
    bool cmpM(const Node& a, const Node& b) {
        return a.M > b.M;
    }
    
    bool cmpE(const Node& a, const Node& b) {
        return a.E > b.E;
    }
    
    void print(int idd) {
        int a[8];
        for(int i = 0; i < n; i ++)
        if(node[i].id == idd) {
            int temp = i;
            a[0] = node[temp].na, a[1] = node[temp].nc, a[2] = node[temp].nm, a[3] = node[temp].ne;
            sort(a, a + 4);
            if(a[0] == node[temp].na)
                printf("%d A
    ", node[temp].na);
            else if(a[0] == node[temp].nc)
                printf("%d C
    ", node[temp].nc);
            else if(a[0] == node[temp].nm)
                printf("%d M
    ", node[temp].nm);
            else
                printf("%d E
    ", node[temp].ne);
        }
    }
    
    int main() {
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; i ++) {
            scanf("%d%d%d%d", &node[i].id, &node[i].C, &node[i].M, &node[i].E);
            node[i].A = (node[i].C + node[i].M + node[i].E) / 3;
            mp[node[i].id] = 1;
        }
        sort(node, node + n, cmpA);
        node[0].na = 1;
        for(int i = 1; i < n; i ++) {
            if(node[i].A == node[i - 1].A)
                node[i].na = node[i - 1].na;
            else node[i].na = i + 1;
        }
        sort(node, node + n, cmpC);
        node[0].nc = 1;
        for(int i = 1; i < n; i ++) {
            if(node[i].C == node[i - 1].C)
                node[i].nc = node[i - 1].nc;
            else node[i].nc = i + 1;
        }
        sort(node, node + n, cmpM);
        node[0].nm = 1;
        for(int i = 1; i < n; i ++) {
            if(node[i].M == node[i - 1].M)
                node[i].nm = node[i - 1].nm;
            else node[i].nm = i + 1;
        }
        sort(node, node + n, cmpE);
        node[0].ne = 1;
        for(int i = 1; i < n; i ++) {
            if(node[i].E == node[i - 1].E)
                node[i].ne = node[i - 1].ne;
            else node[i].ne = i + 1;
        }
        while(m --) {
            int x;
            scanf("%d", &x);
            if(!mp[x])
                printf("N/A
    ");
            else
                print(x);
        }
        return 0;
    }
    

      几乎看了一个下午的搜索 头皮发麻 这个代码觉得写得好麻烦 恰饭去了

  • 相关阅读:
    runtime-给系统已有类添加属性
    解决自定义leftBarButtonItem返回手势失效的方法
    类和对象
    内存拷贝
    响应者链
    属性
    懒加载
    封装思想
    屏幕旋转
    block
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10114567.html
Copyright © 2011-2022 走看看