PAT甲级1012

1012 考试成绩排名

整体难度不大,主要有两个考点:

  1. std::set的使用
  2. 快速确定排名

1主要是套模板。2利用了一个数组进行处理,把某个分数之前的都加1。注意最终排名输出的时候,注意技巧就行。

#include <bits/stdc++.h>
using namespace std;

int C[101], M[101], E[101], A[101];
struct Node {
    string id;
    int C, M, E, A;
    bool operator==(const Node& a) {  // 用于std::set中的键值比较,查找的时候使用
        return id == a.id;
    }
};
struct Node1 {
    char c;
    int n;
};
struct cmp {
    bool operator()(const Node& a, const Node& b)const {
        return a.id < b.id;
    }
};

bool cmp1(const Node1& a, const Node1 &b) {
    if(a.n < b.n) {
        return true;
    } else if(a.n == b.n) {
        return a.c < b.c;
    }
    return false;
}

void deal(const Node& node) {  // 处理各个成绩的排名,基数排序的思想
    for(int i = 0; i < node.C; ++i) {
        ++C[i];
    }
    for(int i = 0; i < node.M; ++i) {
        ++M[i];
    }
    for(int i = 0; i < node.E; ++i) {
        ++E[i];
    }
    for(int i = 0; i < node.A; ++i) {
        ++A[i];
    }
}

void grade(const Node& node) {
    Node1 g[4];
    g[0].n = C[node.C] + 1;
    g[0].c = 'C';
    g[1].n = M[node.M] + 1;
    g[1].c = 'M';
    g[2].n = E[node.E] + 1;
    g[2].c = 'E';
    g[3].n = A[node.A] + 1;
    g[3].c = 'A';
    sort(g, g + 4, cmp1);
    if(g[0].n < g[1].n || g[0].c != 'E') {  // 排序完成后,两个判断就行
        cout << g[0].n << " " << g[0].c << endl;
    } else {
        cout << g[1].n << " " << g[1].c << endl;
    }
}

int main() {
    int N, M;
    set<Node, cmp>S;
    cin >> N >> M;
    Node tmp;
    for(int i = 0; i < N; ++i) {
        cin >> tmp.id >> tmp.C >> tmp.M >> tmp.E;
        tmp.A = (tmp.C + tmp.M + tmp.E) / 3;
        deal(tmp);
        S.insert(tmp);
    }
    for(int i = 0; i < M; ++i) {
        cin >> tmp.id;
        auto it = S.find(tmp);
        if(it == S.end()) {
            cout << "N/A" << endl;
        } else {
            tmp = *it;
            grade(tmp);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值