用一个结构体,里面表示学号,做题数,题号,总分。
先排好成绩,直接用一个结构体进行两个元素的交换即可
注意!如果成绩相同,则按学号升序排列。
继续排序,不过用strcmp函数比较学号即可
#include<stdio.h>
#include<string.h>
struct student{
char name[25];
int m;
int number[15];
int sum;
} Student[1005], Temp;
int N, M, G, value[15];
int main(void)
{
int goodStudent[15];
while (scanf("%d", &N) != EOF && N) {
scanf("%d %d", &M, &G);
for (int i = 0; i < M; i++) {
scanf("%d", &value[i]);
}
for (int i = 0; i < N; i++) {
scanf("%s %d", Student[i].name, &Student[i].m);
Student[i].sum = 0;
for (int j = 0; j < Student[i].m; j++) {
scanf("%d", &Student[i].number[j]);
Student[i].sum += value[Student[i].number[j]-1];
}
}
for (int i = 0; i < N-1; i++) {
int min = Student[i].sum;
int minPos = i;
for (int j = i+1; j < N; j++) {
if (Student[j].sum < min) {
min = Student[j].sum;
minPos = j;
}
}
Temp = Student[i];
Student[i] = Student[minPos];
Student[minPos] = Temp;
}
for (int i = 0; i < N-1; i++) {
for (int j = i+1; j < N; j++) {
if (Student[i].sum == Student[j].sum && strcmp(Student[j].name, Student[i].name) == 1) {
Temp = Student[i];
Student[i] = Student[j];
Student[j] = Temp;
}
}
}
int count = 0, k = 0;
for (int i = 0; i < N; i++) {
if (Student[i].sum >= G) {
goodStudent[k++] = i;
count++;
}
}
printf("%d\n", count);
for (int i = N-1; i >= 0; i--) {
if (Student[i].sum >= G) {
printf("%s %d\n", Student[i].name, Student[i].sum);
}
}
}
return 0;
}