题目链接
解题思路
1、用结构体数组保存输入数据;
2、分别按4个指标排序统计出每个student
的最佳排名和对应课程;
3、再以map<id, struct>
映射一下,方便查找
AC程序(C++)
/**************************
//@Author: 3stone
//@ACM: PAT-A1012 The Best Rank
//@Time: 18/1/27
//@IDE: VS2017
***************************/
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<map>
#define maxn 2010
using namespace std;
struct Stu{
string id;
int C, M, E, A;
int best_rank;
char best_course;
}mp[maxn];
bool cmp1(Stu s1, Stu s2) {
if (s1.C != s2.C) return s1.C > s2.C;
else return s1.id < s2.id;
}
bool cmp2(Stu s1, Stu s2) {
if (s1.M != s2.M) return s1.M > s2.M;
else return s1.id < s2.id;
}
bool cmp3(Stu s1, Stu s2) {
if (s1.E != s2.E) return s1.E > s2.E;
else return s1.id < s2.id;
}
bool cmp4(Stu s1, Stu s2) {
if (s1.A != s2.A) return s1.A > s2.A;
else return s1.id < s2.id;
}
void mySort(int n) {
//按A排序
int rank = 1;
sort(mp, mp + n, cmp4);
mp[0].best_rank = rank;
mp[0].best_course = 'A';
for(int i = 1; i <= n; i++) {
if(mp[i].A != mp[i - 1].A)
rank = i + 1;
mp[i].best_rank = rank;
mp[i].best_course = 'A';
}
//按C排序
rank = 1;
sort(mp, mp + n, cmp1);
if (rank < mp[0].best_rank) {
mp[0].best_rank = rank;
mp[0].best_course = 'C';
}
for(int i = 1; i <= n; i++) {
if(mp[i].C != mp[i - 1].C)
rank = i + 1;
if (rank < mp[i].best_rank) {
mp[i].best_rank = rank;
mp[i].best_course = 'C';
}
}
//按M排序
rank = 1;
sort(mp, mp + n, cmp2);
if (rank < mp[0].best_rank) {
mp[0].best_rank = rank;
mp[0].best_course = 'M';
}
for(int i = 1; i <= n; i++) {
if(mp[i].M != mp[i - 1].M)
rank = i + 1;
if (rank < mp[i].best_rank) {
mp[i].best_rank = rank;
mp[i].best_course = 'M';
}
}
//按E排序
rank = 1;
sort(mp, mp + n, cmp3);
if (rank < mp[0].best_rank) {
mp[0].best_rank = rank;
mp[0].best_course = 'E';
}
//printf("best_course: %c", mp[0].best_course); //输出打表的情况看一看
for(int i = 1; i <= n; i++) {
if(mp[i].E != mp[i - 1].E)
rank = i + 1;
if (rank < mp[i].best_rank) {
mp[i].best_rank = rank;
mp[i].best_course = 'E';
}
// printf(" %c", mp[i].best_course);
}
//printf("\n\n");
}
int main() {
int N, M;
char tempStr[8];
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++) {
scanf("%s %d %d %d", tempStr, &mp[i].C, &mp[i].M, &mp[i].E);
mp[i].A = round( (double)(mp[i].C + mp[i].M + mp[i].E) / 3 );
mp[i].id = tempStr;
}
mySort(N);
map<string, Stu> result;
for (int i = 0; i < N; i++) {
result[mp[i].id] = mp[i];
}
for (int i = 0; i < M; i++) {
scanf("%s", tempStr);
if (result.find(tempStr) == result.end()) {
printf("N/A\n");
continue;
}
printf("%d %c\n", result[tempStr].best_rank, result[tempStr].best_course);
}
system("pause");
return 0;
}