1012 考试成绩排名
整体难度不大,主要有两个考点:
std::set
的使用- 快速确定排名
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;
}