解题代码
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
map<char, int> m;
char refer[4] = { 'C','M','E','A' };
int cnt;
typedef struct scoreandrank {
char course;
int score, rank;
}sar;
struct student {
int id;
sar grade[4];
}s[2000];
typedef struct student stu;
bool cmp1(stu a, stu b) {
return a.grade[cnt].score > b.grade[cnt].score;
}
bool cmp2(sar a, sar b) {
if (a.rank == b.rank) return m[a.course] < m[b.course];
return a.rank < b.rank;
}
int main()
{
int N, M;
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++) {
scanf("%d%d%d%d", &s[i].id, &s[i].grade[0].score, &s[i].grade[1].score, &s[i].grade[2].score);
s[i].grade[3].score = (s[i].grade[0].score+ s[i].grade[1].score+ s[i].grade[2].score) / 3;
for (int j = 0; j < 4; j++) s[i].grade[j].course = refer[j];
}
for (cnt = 0; cnt < 4; cnt++) {
sort(s, s + N, cmp1);
for (int i = 0; i < N; i++) {
if (i && s[i].grade[cnt].score == s[i - 1].grade[cnt].score)
s[i].grade[cnt].rank = s[i - 1].grade[cnt].rank;
else s[i].grade[cnt].rank = i + 1;
}
}
m['A'] = 1; m['C'] = 2; m['M'] = 3; m['E'] = 4;
for (int i = 0; i < N; i++) sort(s[i].grade, s[i].grade + 4, cmp2);
int temp, j;
bool flag = false;
for (int i = 0; i < M; i++) {
scanf("%d", &temp);
if (!flag) flag = true;
else printf("\n");
for ( j = 0; j < N; j++)
if (s[j].id == temp) {
printf("%d %c", s[j].grade[0].rank, s[j].grade[0].course);
break;
}
if (j == N) printf("N/A");
}
return 0;
}
测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/92a53c728f5b4ac8dcbeaab925c7e41b.png)
问题整理
1.排序算法。
2.该题的score和rank其实可以用同一个空间先后存储,用score获得了rank后score在该题就失效了。
3.输入时可以直接安排好各科目的顺序,在最后对每个人的科目做排序时,便只需寻找最小的数的位置,便可以直接输出,而不用再次排序了。