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

思路

  • 用结构体存放学生的数据,string类型存放学生id,best存放学生排名最好的科目(科目的优先级是A>C>M>E),所以best就是对应这个优先级字符的下标,scores[4]表示学生4门成绩,注意用scores[0]存放平均分,rank[4]对应于4门成绩的排名。

  • 此外,用map判断是学生是否存在成绩,键值对为(id,下标);存放下标是为了记录学生在students[2000]数组里面的位置,因为每次排序后位置都会发生变化

  • 自定义的cmp比较函数,sort的第三个参数是比较函数,该函数只能传入两个参数,所以定义一个全局index传入方便比较学生的各个成绩

  • map的插入方式:

    扫描二维码关注公众号,回复: 12949923 查看本文章
    exist.insert(pair<string,int>(students[i].stu_id, i)); // 记录students数组的下标
    
  • map的判断是否存在key:如果存在key,count函数则返回1,否则返回0

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

注意点:

  • 学生单门的成绩相同时的排名,例如1,1,3,4,5而不是1,1,2,3,4。
  • 对于平均分,不用四舍五入也能通过样例,PAT和牛客网(id是字符串)均可

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

猜你喜欢

转载自blog.csdn.net/weixin_42100456/article/details/108926149