[PAT] A1012 The Best Rank

(排序;排名的实现)

题目大意

现已知n个考生的3门分数,平均分可以按照这三门算出来。然后分别对这四个分数从高到低排序,这样对每个考生来说有4个排名。m个查询,对于每一个学生id,输出当前id学生的最好的排名和它对应的分数,如果名次相同,按照A>C>M>E的顺序输出。如果当前id不存在,输出N/A。
题目链接

思路

将输入数据存好,用结构体(参数为id、分数和排名);输入时顺便计算平均分(这里因为不涉及到具体数值,可以直接用总分;若是平均分记得四舍五入)。
用sort按分数排序从大到小排序。
计算排名的方法:第一项的排名是1,其余的循环一遍,若当前分数与上一个相同,当前排名就等于上一个的排名;否则排名等于下标i+1。
按照ACME的比较被询问的各个人的排名,若大于才交换,可以保证相同的时候是按照ACME的优先级输出的。

化简

1、用一个结构体存储学生的id(题目说是6位数字,可以直接存成整型)、四门成绩、四门排名、最好的排名的对应的科目下标

struct node {
    int id, best;
    int score[4], rank[4];
}stu[2002];

2、存储的时候就按照ACME的顺序存储可以简化程序逻辑
3、用exist数组保存当前id是否存在,这个id对应的stu结构体的下标是多少。用i+1可以保证为0的都是不存在的可以直接输出N/A,其余不为0的保存的值是对应的结构体index + 1的值(在输出时结构体下标记得减一)

AC代码(未化简)

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <cstdio>
#include<algorithm>
#include <cstring>
using namespace std;
#define N 2000
struct A {
    int score, rank;
    string id;
}aaa[N];
struct C {
    int score, rank;
    string id;
}ccc[N];
struct M {
    int score, rank;
    string id;
}mmm[N];
struct E {
    int score, rank;
    string id;
}eee[N];
bool cmpa(A a, A b) { return a.score > b.score; }
bool cmpc(C a, C b) { return a.score > b.score; }
bool cmpm(M a, M b) { return a.score > b.score; }
bool cmpe(E a, E b) { return a.score > b.score; }
int main() {
    int n, m, i, j;
    scanf("%d%d", &n, &m);
    for (i = 0;i < n;i++) {
        string tid;
        cin >> tid;
        scanf("%d", &ccc[i].score);
        scanf("%d", &mmm[i].score);
        scanf("%d", &eee[i].score);
        aaa[i].score = ccc[i].score + mmm[i].score + eee[i].score;
        aaa[i].id = ccc[i].id = mmm[i].id = eee[i].id = tid;
    }
    sort(aaa, aaa + n, cmpa);
    sort(mmm, mmm + n, cmpm);
    sort(ccc, ccc + n, cmpc);
    sort(eee, eee + n, cmpe);
    aaa[0].rank = mmm[0].rank = ccc[0].rank = eee[0].rank = 1;
    for (i = 1;i < n;i++) {
        if (aaa[i].score == aaa[i - 1].score)
            aaa[i].rank = aaa[i - 1].rank;
        else aaa[i].rank = i + 1;
        if (mmm[i].score == mmm[i - 1].score)
            mmm[i].rank = mmm[i - 1].rank;
        else mmm[i].rank = i + 1;
        if (ccc[i].score == ccc[i - 1].score)
            ccc[i].rank = ccc[i - 1].rank;
        else ccc[i].rank = i + 1;
        if (eee[i].score == eee[i - 1].score)
            eee[i].rank = eee[i - 1].rank;
        else eee[i].rank = i + 1;
    }
    for (i = 0;i < m;i++) {
        string checkid;
        cin >> checkid;
        int bestrank = n + 1;
        char bestcourse;
        bool flag = false;
        for (j = 0;j < n;j++) {
            if (aaa[j].id == checkid) {
                flag = true;
                bestrank = aaa[j].rank;
                bestcourse = 'A';
                break;
            }
        }
        for (j = 0;j < n;j++) {
            if (ccc[j].id == checkid) {
                flag = true;
                if (bestrank > ccc[j].rank) {
                    bestrank = ccc[j].rank;
                    bestcourse = 'C';
                }
                break;
            }
        }
        for (j = 0;j < n;j++) {
            if (mmm[j].id == checkid) {
                flag = true;
                if (bestrank > mmm[j].rank) {
                    bestrank = mmm[j].rank;
                    bestcourse = 'M';
                }
                break;
            }
        }
        for (j = 0;j < n;j++) {
            if (eee[j].id == checkid) {
                flag = true;
                if (bestrank > eee[j].rank) {
                    bestrank = eee[j].rank;
                    bestcourse = 'E';
                }
                break;
            }
        }
        if (flag == false)printf("N/A\n");
        else printf("%d %c\n", bestrank, bestcourse);
    }
    return 0;
}

原文链接: https://www.cnblogs.com/yue36/p/13332890.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    [PAT] A1012 The Best Rank

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/367677

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 下午5:54
下一篇 2023年3月2日 下午5:55

相关推荐