Description
自古以来,期末成绩排名一直困扰着人类社会。现在给出N个人的姓名、理论考成绩以及实践考成绩。总分等于理论考和实践考成绩之和,请按照总分排序并输出。题目保证分数均为正整数,两两之间总分互不相同。
Input
第一行一个整数T,表示T组数据。(0<T<100)
每组测试数据第一行为一个整数N代表接下来有N条数据(0<N<100)。
接下来N行,每行给出一个英文名字(长度小于30),以及这个名字对应的理论成绩与实践成绩(均为正整数)。
Output
请输出排序后的姓名列表。
Samples
input
1
3
YYY 10 10
AAA 1 1
SSS 3 3
output
YYY
SSS
AAA
AC代码
#include <stdio.h>
#include <string.h>
struct Student {
char name[30];
int score;
};
int compare(const void* a, const void* b) {
return ((struct Student*)b)->score - ((struct Student*)a)->score;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
int N;
scanf("%d", &N);
struct Student student[100];
int theory, practice;//存放理论和实践成绩
for (int i = 0; i < N; i++) {
scanf("%s %d %d", student[i].name, &theory, &practice);
student[i].score = theory + practice;
}
qsort(student, N, sizeof(struct Student), compare);
for (int i = 0; i < N; i++) {
printf("%s\n", student[i].name);
}
}
return 0;
}
注意:struct要写在compare前面