PAT 1012 The Best Rank(结构体排序)

该程序用于评估计算机科学一年级学生的成绩表现,考虑C语言、数学和英语三门课程的成绩,并输出每个学生在这些科目及平均分中的最佳排名。输入包括学生数量、查询排名的学生数量以及每个学生的ID和各科成绩。输出是每个查询学生最佳排名及其对应科目。优先级为A(平均分)>C(编程语言)>M(数学)>E(英语)。
摘要由CSDN通过智能技术生成

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.

排序模拟,注意不要爆栈,考虑到并列的情况。

改了好久的bug,脑壳疼,代码如下:

#include<iostream>
using namespace std;
struct student{
    string ID;
    int point_A;
    int rank_A;
    int point_C;
    int rank_C;
    int point_M;
    int rank_M;
    int point_E;
    int rank_E;
}students[2000];
int main()
{
    int N,M;
    cin>>N>>M;
    for(int i=0;i<N;i++){
        cin>>students[i].ID;
        cin>>students[i].point_C;
        cin>>students[i].point_M;
        cin>>students[i].point_E;
        students[i].point_A=(students[i].point_C+students[i].point_E+students[i].point_M)/3;
    }
    for(int i=0;i<N;i++){
        int ranking=1;
        for(int k=0;k<N;k++)
            if(students[i].point_A<students[k].point_A)
                ranking++;
        students[i].rank_A=ranking;
        ranking=1;
        for(int k=0;k<N;k++)
            if(students[i].point_C<students[k].point_C)
                ranking++;
        students[i].rank_C=ranking;
        ranking=1;
        for(int k=0;k<N;k++)
            if(students[i].point_M<students[k].point_M)
                ranking++;
        students[i].rank_M=ranking;
        ranking=1;
        for(int k=0;k<N;k++)
            if(students[i].point_E<students[k].point_E)
                ranking++;
        students[i].rank_E=ranking;
    }
    string ID;
    for(int i=0;i<M;i++){
        bool flag=false;
        cin>>ID; 
        for(int k=0;k<N;k++){     
            if(ID==students[k].ID){
                flag=true;
                for(int p=0;p<N;p++){
                    if(p+1==students[k].rank_A){
                        cout<<p+1<<" "<<"A"<<endl;
                        break;
                    }
                    else if(p+1==students[k].rank_C){
                        cout<<p+1<<" "<<"C"<<endl;
                        break;
                    }
                    else if(p+1==students[k].rank_M){
                        cout<<p+1<<" "<<"M"<<endl;
                        break;
                    }
                    else if(p+1==students[k].rank_E){
                        cout<<p+1<<" "<<"E"<<endl;
                        break;
                    }  
                }
            }
        if(flag)
            break;     
        }
        if(!flag){
            cout<<"N/A"<<endl;
        }
    } 
    return 0;
}

样例结果:

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值