PAT A1012. The Best Rank

占个坑先,改日详写

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

struct Student
{
    int id;
    int C;
    int M;
    int E;
    int A;
    int best_rank;
    char symbol;

}student[2010];

int cmp_A(Student a,Student b)
{
    if(a.id!=b.id) return a.A>b.A;
    else return a.id <b.id;
}

int cmp_C(Student a,Student b)
{
    if(a.id!=b.id) return a.C>b.C;
    else return a.id <b.id;
}

int cmp_M(Student a,Student b)
{
    if(a.id!=b.id) return a.M>b.M;
    else return a.id <b.id;
}

int cmp_E(Student a,Student b)
{
    if(a.id!=b.id) return a.E>b.E;
    else return a.id <b.id;
}

int search_id(int id,int N)
{
    for(int i =0;i<N;i++)
    {
        if(student[i].id==id)
            return i;
    }

    return -1;
}



int main()
{
    int N,M;
    int search_list[2010];
    scanf("%d %d",&N,&M);
    for(int i = 0;i<N;i++)
    {
        scanf("%d%d%d%d",&student[i].id,&student[i].C,&student[i].M,&student[i].E);
        student[i].A = (student[i].C+student[i].M+student[i].E)/3;
    }

    for(int i = 0;i<M;i++)
    {
        scanf("%d",&search_list[i]);
    }

    sort(student,student+N,cmp_A);
    for(int i = 0;i<N;i++)
    {
        int tmp_i = i;
        while(tmp_i>0&&student[tmp_i].A == student[tmp_i-1].A)
        {
                tmp_i--;
        }
        student[i].best_rank = tmp_i+1;
        student[i].symbol = 'A';
    }
    sort(student,student+N,cmp_C);
    for(int i = 0;i<N;i++)
    {
        int tmp_i = i;
        while(tmp_i>0&&student[tmp_i].C == student[tmp_i-1].C)
        {
                tmp_i--;
        }
        if(student[i].best_rank>(tmp_i+1))
        {
            student[i].best_rank = tmp_i+1;
            student[i].symbol = 'C';
        }
    }
    sort(student,student+N,cmp_M);
    for(int i = 0;i<N;i++)
    {
        int tmp_i = i;
        while(tmp_i>0&&student[tmp_i].M == student[tmp_i-1].M)
        {
                tmp_i--;
        }
        if(student[i].best_rank>(tmp_i+1))
        {
            student[i].best_rank = tmp_i+1;
            student[i].symbol = 'M';
        }
    }

    sort(student,student+N,cmp_E);
    for(int i = 0;i<N;i++)
    {
        int tmp_i = i;
        while(tmp_i>0&&student[tmp_i].E == student[tmp_i-1].E)
        {
                tmp_i--;
        }
        if(student[i].best_rank>(tmp_i+1))
        {
            student[i].best_rank = tmp_i+1;
            student[i].symbol = 'E';
        }
    }



    int tmp;
    for(int i = 0;i<M;i++)
    {
        tmp = search_id(search_list[i],N);
        if(tmp>=0)
            printf("%d %c\n",student[tmp].best_rank,student[tmp].symbol);
        else
            printf("N/A\n");
    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37637619/article/details/89892011