PAT Grade 1012 The Best Rank (25 minutes) (structure sorting)

Meaning of the questions:

In order to evaluate the performance of our CS majors in the first year, we only consider the results of their three courses: C - C programming language, M - mathematics (calculus or linear algebra) and E - English. At the same time, we encourage students to emphasize their best team - that is,
the four rank and grade point average of three courses, we print each student's best rankings.

For example, C, M, E and A - 4 student achievement as follows:

Of CMEA StudentID
310101 98 85 88 90
310 102 70 95 88 84
310 103 82 87 94 88
310104 91 is 91 is 91 is 91 is
then the best ranking are first of all students, as do the first aspect preferably in the C programming language, while the first two is the second math, English third, the average third.

Entry

Each input file contains a test case. In each case the start line contains two numbers N and M (<= 2000) and
are the number of students and the number of students. Then press the N rows, each containing a 6-digit student ID, followed by an integer of three levels of the student's C, M and E sequence (in the [0,100] range within). Then there is M rows, each row containing student ID
.

Export

For each M student, one-line print his / her best ranking, as well as the corresponding symbol ranked separated by a space.

Prioritization method for ranking A> C> M> E. Therefore, if the best students get the same ranking, there are two or more ways, with the highest priority ranking output.
If the student does not score on the list, only the output "N / A" can be.

Ideas:

1. I set the ID became String, in fact, would have been an int is also possible. That's right before judging whether the M has appeared in a string causing trouble. In this regard, node2 structure I started to set in, give each rank rank assigned to 99999, the way the strings put in the queue, and then sort, and then traverse the queue, there has never been before if the new string, then its rank is also 99999, outputs N / a

2. Sorting made me big head, 113,336 rather than 112,223, nor is 123456. First, there would have been sort Comparator> = on it, but why mistakes. The reason: https://blog.csdn.net/aichirourou_66/article/details/80928249

I had to get myself sort of ranking, thereby introducing a k stored on a ranking. j = k, instead of M [a [i-1] .s] .rank, this place careless lost a lot of time.

3. Sort I actually wrote a repeated four times, in fact, put a loop on it. . .

AC Code:

#include <bits / STDC ++ H.>
 the using  namespace STD;
 int n-, m;
 struct Node { // store student information 
    String S;
     int C;
     int mA;
     int E;
     Double AV; 
} A [ 5005 ];
 struct node2 / / storing rank information 
{
     int rank; // rank 
    char course; // course 
    node2 ( int _Rank = 99999 , char , _course = ' A ' )
    { 
        Rank = _Rank; 
        Course = , _course; 
    } 
}; 
Map < String , node2> M; // The ID correspondence information and rank 
Queue < String > Q; // for string M input put back 

BOOL cmp_av ( const Node X &, const Node & Y) {
     return x.av> y.av; // not> =, mistakes will 
}
 BOOL cmp_c ( const Node X &, const Node & Y) {
     return XC> YC; 
} 
BOOL cmp_ma(const node &x, const node &y) {
    return x.ma > y.ma;
}
bool cmp_e(const node &x, const node &y) {
    return x.e > y.e;
}
string ss;
int main()
{
    cin>>n>>m;
    while(!Q.empty()) Q.pop();
    for(int i=1;i<=n;i++){
        cin>>a[i].s>>a[i].c>>a[i].ma>>a[i].e;
        a[i].av= Int ((A [I] .c + A [I] .ma + A [I] .E) * 1.0 / . 3 + 0.5 ); 
        M [A [I] .s] = node2 ( 99999 , ' A ' ); // give a default rank information 
    } 
    
    for ( int I = . 1 ; I <= m; I ++ ) 
    { 
        CIN >> SS; 
        Q.push (SS); // first into them, after which they again traverse 
        M [a [I] .s] = node2 ( 99999 , ' a ' ); // give a default rank information 
    } 
    Sort (a + . 1, A + . 1 + n-, cmp_av);
     int K = . 1 ; // for ranking provided, 113,336, rather than 112,223, nor is 123456 
    for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        int J = I;
         IF (I> . 1 && a [I] == .av a [I- . 1 ] .av) { 
            J = k; // for the foregoing noun 
        }
         the else { 
            k = J; // terms of j, k and update 
        }
         IF (M [A [I] .s] .rank> J) { 
            M [A [I] .s] .rank =j;
            M[a[i].s].course='A';
        }
//        cout<<j<<" "<<a[i].s<<" "<<a[i].av<<endl;
    }

    sort(a+1,a+1+n,cmp_c);
//    cout<<endl;
    k=1;
    for(int i=1;i<=n;i++)
    {
        int j=i;
        if(i>1 && a[i].c==a[i-1].c){
            j=k;
        }
        else{
            k=j;
        }
        if(M[a[i].s].rank>j){
            M[a[i].s].rank=j;
            M[a[i].s].course='C';
        }
//        cout<<j<<" "<<a[i].s<<" "<<a[i].c<<endl;
    }
    
    sort(a+1,a+1+n,cmp_ma);
//    cout<<endl;
    k=1;
    for(int i=1;i<=n;i++)
    {
        int j=i;
        if(i>1 && a[i].ma==a[i-1].ma){
            j=k;
        }
        else{
            k=j;
        }
        if(M[a[i].s].rank>j){
            M[a[i].s].rank=j;
            M[a[i].s].course='M';
        }
//        cout<<j<<" "<<a[i].s<<" "<<a[i].ma<<endl;
    }
    
    sort(a+1,a+1+n,cmp_e);
//    cout<<endl;1
    k =;
    for(int i=1;i<=n;i++)
    {
        int j=i;
        if(i>1 && a[i].e==a[i-1].e){
            j=k;
        }
        else{
            k=j;
        }
        if(M[a[i].s].rank>j){
            M[a[i].s].rank=j;
            M[a[i].s].course='E';
        }
//        cout<<j<<" "<<a[i].s<<" "<<a[i].e<<endl;
    }
    while(!Q.empty())
    {
        ss = Q.front();
        Q.pop();
        //cout<<ss<<" "<<M[ss].rank<<endl;
        if(M[ss].rank==99999){//不存在的情况 
            cout<<"N/A"<<endl;
        }
        else{
            cout<<M[ss].rank<<" "<<M[ss].course<<endl;
        }
    }
    return 0;
 } 

 

Guess you like

Origin www.cnblogs.com/caiyishuai/p/11279935.html