PAT-1012 The Best Rank

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.

Input Specification:

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 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

Ideas

  • Use structure to store student data, string type to store student id, best to store the subject with the best student ranking (subject priority is A>C>M>E), so best is the subscript corresponding to this priority character, scores [4] represents the student's 4 scores, pay attention to use scores[0] to store the average score, rank[4] corresponds to the ranking of the 4 scores.

  • In addition, use map to determine whether the student has a grade, the key-value pair is (id, subscript); the subscript is stored to record the position of the student in the students[2000] array, because the position will change after each sorting

  • Custom cmp comparison function, the third parameter of sort is the comparison function, this function can only pass in two parameters, so define a global index to pass in to facilitate the comparison of the students’ scores

  • How to insert the map:

    exist.insert(pair<string,int>(students[i].stu_id, i)); // 记录students数组的下标
    
  • The map judges whether there is a key: if there is a key, the count function returns 1, otherwise it returns 0

    (exist.count(id) !=0){
    

important point:

  • The ranking when students have the same score in a single subject, for example, 1,1,3,4,5 instead of 1,1,2,3,4.
  • For the average score, the sample can be passed without rounding, both PAT and Niuke.com (id is a string)

Code

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

struct stu
{
    
    
    int scores[4];
    string stu_id;
    int rank[4];
    int best; // 最好的科目
};
stu students[3000];
map<string,int>exist;
int index = -1;
bool cmp(stu a,stu b){
    
      // 只能接受两个参数
    return a.scores[index]>b.scores[index];
}
int main()
{
    
    
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
    
    
       cin >> students[i].stu_id;
       int sum = 0;
       for (int j=1; j<=3; j++){
    
    
           cin >> students[i].scores[j];
           sum += students[i].scores[j];
       }
       students[i].scores[0] = sum/3;
    }
    for (index=0; index<=3; index++){
    
        // 计算每个科目的排名
        sort(students, students+n,cmp);
        students[0].rank[index] = 1;
        for(int j=1; j<n;j++){
    
    
            students[j].rank[index] = j+1;
            if (students[j].scores[index] == students[j-1].scores[index])
                students[j].rank[index] = students[j-1].rank[index];  // 注意不要把rank打成scores
        }
    }
    for(int i=0; i<n; i++){
    
    
        exist.insert(pair<string,int>(students[i].stu_id, i)); // 记录students数组的下标
        int min = students[i].rank[0];
        students[i].best = 0;
        for(int j=1; j<4; j++){
    
    
            if (min > students[i].rank[j]){
    
    
                min = students[i].rank[j];
                students[i].best = j;
            }
        }
    }
    // for (int i=0; i<n; i++){
    
    
    //     cout <<students[i].stu_id << " " << students[i].best << " " <<students[i].rank[students[i].best] <<endl;
    // }
    // cout <<endl;
    // for (auto it= exist.begin(); it != exist.end(); it++){
    
    
    //     cout << it->first << " " << it->second << endl;
    // }
    char arr[] = {
    
    'A','C','M','E'};
    string id;
    for (int i=0; i<m; i++){
    
    
        cin >> id;
        if (exist.count(id) !=0){
    
    
            int index = exist[id];
            // cout << "index:" <<index << endl;
            cout << students[index].rank[students[index].best] << " " <<arr[students[index].best] << endl;
        }
        else{
    
    
            cout << "N/A" << endl;
        }
    }
    // cout << students[exist["310101"]].stu_id << " " << students[exist["310101"]].best <<endl;

    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_42100456/article/details/108926149