目录
原题
思路
依次按照平均分、C语言、数学、英语分数对学生进行排序。然后依次在按平均分、C语言、数学、英语排好顺序的数组中找到该学生排名,记录该学生的最好排名及其科目。
代码
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <algorithm>
using namespace std;
struct student {
char id[7];
double c_lan, math, english, aveg;
};
bool cmpc(student s1, student s2);
bool cmpm(student s1, student s2);
bool cmpe(student s1, student s2);
bool cmpa(student s1, student s2);
int main(void) {
setvbuf(stdin, new char[1 << 20], _IOFBF, 1 << 20);
setvbuf(stdout, new char[1 << 20], _IOFBF, 1 << 20);
int n, m;
scanf("%d %d", &n, &m);
student *arraya = new student[n], *arrayc = new student[n], *arraym = new student[n], *arraye = new student[n];
student *array[4] = { arraya, arrayc, arraym, arraye };
char **checklist = new char*[m];
int i, j, k;
for (i = 0; i < m; i++) {
checklist[i] = new char[7];
}
bool(*temp[4])(student, student) = { cmpa, cmpc, cmpm, cmpe };
bool(*cmp)(student, student);
for (i = 0; i < n; i++) {
scanf("%s %lf %lf %lf", arraya[i].id, &(arraya[i].c_lan), &(arraya[i].math), &(arraya[i].english));
arraya[i].aveg = (arraya[i].c_lan + arraya[i].math + arraya[i].english) / 3;
arrayc[i] = arraym[i] = arraye[i] = arraya[i];
}
for (i = 0; i < m; i++) {
scanf("%s", checklist[i]);
}
for (i = 0; i < 4; i++) {
sort(array[i], array[i] + n, temp[i]);
}
int rank, course;
for (i = 0; i < m; i++) {
rank = INT_MAX;
for (j = 0; j < 4; j++) {
for (k = 0; k < n; k++) {
if (!strcmp(array[j][k].id, checklist[i])) {
switch (j) {
case 0: while (k > 0 && array[j][k - 1].aveg == array[j][k].aveg) { k--; } break;
case 1: while (k > 0 && array[j][k - 1].c_lan == array[j][k].c_lan) { k--; } break;
case 2: while (k > 0 && array[j][k - 1].math == array[j][k].math) { k--; } break;
case 3: while (k > 0 && array[j][k - 1].english == array[j][k].english) { k--; } break;
default:break;
}
if (k + 1 < rank) {
rank = k + 1;
course = j;
}
break;
}
}
}
if (rank == INT_MAX) {
puts("N/A");
}
else {
printf("%d ", rank);
switch (course) {
case 0: putchar('A'); break;
case 1: putchar('C'); break;
case 2: putchar('M'); break;
case 3: putchar('E'); break;
default: break;
}
putchar('\n');
}
}
for (i = 0; i < 4; i++) {
delete[] array[i];
}
for (i = 0; i < m; i++) {
delete[] checklist[i];
}
delete[] checklist;
return 0;
}
bool cmpc(student s1, student s2) {
return s1.c_lan > s2.c_lan;
}
bool cmpm(student s1, student s2) {
return s1.math > s2.math;
}
bool cmpe(student s1, student s2) {
return s1.english > s2.english;
}
bool cmpa(student s1, student s2) {
return s1.aveg > s2.aveg;
}