码迷,mamicode.com
首页 > 其他好文 > 详细

1012 The Best Rank (25 分)

时间:2019-01-01 23:56:06      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:sim   ini   ogr   lin   order   sid   priority   cal   output   

1012 The Best Rank (25 分)

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

注意点:
  1、可能存在两个或多个学生分数相同的情况,排名顺序是1、1、3、4、5而不是1、2、3、4、5,所以不是简单的数组下标相加1
2解决这个问题我最开始是想如果前后两个分数相同,则后一个排名等于前一个的排名。这是错误的!!!因为前一个学生的排名很可能在这次排序中
没有进行更新。
#include<iostream>
#include<sstream>
#include<algorithm>
#include<map>
#include<cmath>
#include<cstdio>
using namespace std;
struct Student
{
    string id;
    int c;
    int m;
    int e;
    int a;
};
bool compareByA(Student &A,Student &B)
{
    return A.a>B.a;
}
bool compareByC(Student &A,Student &B)
{
    return A.c>B.c;
}
bool compareByM(Student &A,Student &B)
{
    return A.m>B.m;
}
bool compareByE(Student &A,Student &B)
{
    return A.e>B.e;
}
map<string,int> rate;
map<string,char> clazz;
void updateRate(Student stus[],int n,char ch)
{
    int m=0;
    for(int i=0; i<n; i++)
    {
        if(ch==A)
        {
            if(i>0&&(stus[i].a==stus[i-1].a))
                m=rate[stus[i-1].id];
            else
                m=i+1;
        }
        else if(ch==C)
        {
            if(i>0&&(stus[i].c==stus[i-1].c))
                m=rate[stus[i-1].id];
            else
                m=i+1;
        }
        else if(ch==M)
        {
            if(i>0&&(stus[i].m==stus[i-1].m))
                m=rate[stus[i-1].id];
            else
                m=i+1;
        }
        else
        {
            if(i>0&&(stus[i].e==stus[i-1].e))
                m=rate[stus[i-1].id];
            else
                m=i+1;
        }
        string id=stus[i].id;
        if(rate[id]>m)
        {
            rate[id]=m;
            clazz[id]=ch;
        }
    }
}

int main()
{
    int n,m;


    cin>>n>>m;
    Student stus[n];
    for(int i=0; i<n; i++ )
    {
        cin>>stus[i].id>>stus[i].c>>stus[i].m>>stus[i].e;
        stus[i].a=(stus[i].c+stus[i].e+stus[i].m+0.5)/3;
        rate[stus[i].id]=10000;
    }
    string id[m];
    for(int i=0; i<m; i++)
    {
        cin>>id[i];
    }
    sort(stus,stus+n,compareByA);
    //cout<<stus[0].id<<" "<<stus[0].a<<" "<<stus[0].c<<endl;
    updateRate(stus,n,A);
    sort(stus,stus+n,compareByC);
    updateRate(stus,n,C);
    sort(stus,stus+n,compareByM);
    updateRate(stus,n,M);
    sort(stus,stus+n,compareByE);
    updateRate(stus,n,E);
    for(int i=0; i<m; i++)
    {
        if(rate.find(id[i])==rate.end())
            cout<<"N/A"<<endl;
        else
            cout<<rate[id[i]]<<" "<<clazz[id[i]]<<endl;
    }
    return 0;
}

 


  
  

1012 The Best Rank (25 分)

标签:sim   ini   ogr   lin   order   sid   priority   cal   output   

原文地址:https://www.cnblogs.com/zhanghaijie/p/10206499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!