PAT(甲级)1012 The Best Rank(排序)

Description

题目大意:给出n个人的成绩,m次询问,求这个人最好的成绩排名

Input

n m
n个人的信息,m次询问ID

Output

每个人的最好排名和科目

解题思路

算法标签:纯粹的排序,按照题意即可

代码

// TSWorld
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N =10005;

struct Student
{
    string ID;
    int Clangue;
    int Math;
    int English;
    int Average;

    int ranka;
    int rankc;
    int rankm;
    int ranke;
}students[N];

// 比较Average
bool cmp1(struct Student a,struct Student b)
{
    return a.Average > b.Average;
}

bool cmp2(struct Student a,struct Student b)
{
    return a.Clangue > b.Clangue;
}

bool cmp3(struct Student a,struct Student b)
{
    return a.Math > b.Math;
}

bool cmp4(struct Student a,struct Student b)
{
    return a.English > b.English;
}

void search(int place)
{
    int rank = students[place].ranka;
    int number = 1;

    if(students[place].rankc < rank)
    {
        rank = students[place].rankc;
        number = 2;
    }

    if(students[place].rankm < rank)
    {
        rank = students[place].rankm;
        number = 3;
    }

    if(students[place].ranke < rank)
    {
        rank = students[place].ranke;
        number = 4;
    }

    cout<<rank<<" ";

    if(number == 1)
        cout<<"A"<<endl;
    else if(number == 2)
        cout<<"C"<<endl;
    else if(number == 3)
        cout<<"M"<<endl;
    else
        cout<<"E"<<endl;
}

int main()
{
    int n = 0,m = 0;
    string ID;
    int Math = 0,English = 0,Clangue = 0;

    cin>>n>>m;
    for(int i = 1;i <= n;i++) {
        cin>>ID;
        cin>>Clangue>>Math>>English;
        students[i].ID = ID;
        students[i].Clangue = Clangue;
        students[i].Math = Math;
        students[i].English = English;
        students[i].Average = int(double(Clangue+Math+English)/3 + 0.5);
    }

    sort(students+1,students+n+1,cmp1);
    for(int i = 1;i <= n;i++) {
        if(students[i].Average == students[i-1].Average)
            students[i].ranka = students[i-1].ranka;
        else
            students[i].ranka = i;
    }

    sort(students+1,students+n+1,cmp2);
    for(int i = 1;i <= n;i++) {
        if(students[i].Clangue == students[i-1].Clangue)
            students[i].rankc = students[i-1].rankc;
        else
            students[i].rankc = i;
    }

    sort(students+1,students+n+1,cmp3);
    for(int i = 1;i <= n;i++) {
        if(students[i].Math == students[i-1].Math)
            students[i].rankm = students[i-1].rankm;
        else
            students[i].rankm = i;
    }

    sort(students+1,students+n+1,cmp4);
    for(int i = 1;i <= n;i++) {
        if(students[i].English == students[i-1].English)
            students[i].ranke = students[i-1].ranke;
        else
            students[i].ranke = i;
    }

    for(int i = 1;i <= m;i++) {
        cin>>ID;
        bool isok = false;
        for(int j = 1;j <= n;j++)
            if(students[j].ID == ID)
            {
                search(j);
                isok = true;
                break;
            }
        if(!isok)
            cout<<"N/A"<<endl;
    }    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值