1012 The Best Rank (25 分)
此题难在结构体的设计,如何联系各个字段。=》此种类型多用结构体
#include <cstdio>
#include <algorithm>
using namespace std;
struct node { // 使用结构体存储
int id, best; // id记录学号,best记录最好的排列科目
int score[4], rank[4];
}stu[2005];
int exist[1000000], flag = -1;
bool cmp1(node a, node b) {return a.score[flag] > b.score[flag];} // 降序排列
int main() {
int n, m, id;
scanf("%d %d", &n, &m); // 输入学生个数n和询问次数m
for(int i = 0; i < n; i++) {
// 依次录入学号+3门成绩
scanf("%d %d %d %d", &stu[i].id, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5; // 平均分四舍五入
}
for(flag = 0; flag <= 3; flag++) {
sort(stu, stu + n, cmp1); // 分别按照每一项对结构体排序
stu[0].rank[flag] = 1; // 初始化排名第一名
for(int i = 1; i < n; i++) {
stu[i].rank[flag] = i + 1; // 已经排序完成,则第i个同学排名为第i名,形成并列的话 1 1 3 4
if(stu[i].score[flag] == stu[i-1].score[flag]) // 分数一样则排名一样
stu[i].rank[flag] = stu[i-1].rank[flag];
}
}
for(int i = 0; i < n; i++) {
exist[stu[i].id] = i + 1; // 学号桶,学号---下标
stu[i].best = 0;
int minn = stu[i].rank[0];
for(int j = 1; j <= 3; j++) {
if(stu[i].rank[j] < minn) { // 寻找排名最靠前的科目
minn = stu[i].rank[j];
stu[i].best = j;
}
}
}
char c[5] = {'A', 'C', 'M', 'E'};
for(int i = 0; i < m; i++) {
scanf("%d", &id); // 输入学号
int temp = exist[id]; // 找到下标
if(temp) {
int best = stu[temp-1].best;
printf("%d %c\n", stu[temp-1].rank[best], c[best]);
} else {
printf("N/A\n");
}
}
return 0;
}