学生的信息由姓名(不超过20个字符)、性别、年龄、c语言成绩、数学成绩、英语成绩组成,输入若干同学的基本信息,按平均分数降序排序,最后输出基本信息(如果平均分数相同,按输入的顺序输出)。
时间: 2023-04-22 13:06:51 浏览: 125
可以使用结构体来表示学生的信息,定义如下:
struct Student {
char name[21];
char gender;
int age;
int c_score;
int math_score;
int english_score;
float average_score;
};
然后,可以定义一个数组来存储多个学生的信息,输入学生的基本信息,并计算每个学生的平均分数:
int n; // 学生数量
scanf("%d", &n);
Student students[n];
for (int i = 0; i < n; i++) {
scanf("%s %c %d %d %d %d", students[i].name, &students[i].gender, &students[i].age, &students[i].c_score, &students[i].math_score, &students[i].english_score);
students[i].average_score = (students[i].c_score + students[i].math_score + students[i].english_score) / 3.0;
}
接下来,可以使用冒泡排序或快速排序等算法,按平均分数降序排序:
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].average_score < students[j+1].average_score) {
Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
最后,按顺序输出排序后的学生信息:
for (int i = 0; i < n; i++) {
printf("%s %c %d %d %d %d %.2f\n", students[i].name, students[i].gender, students[i].age, students[i].c_score, students[i].math_score, students[i].english_score, students[i].average_score);
}
如果有多个学生的平均分数相同,按输入的顺序输出,可以在排序时加入判断条件,例如:
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].average_score < students[j+1].average_score || (students[j].average_score == students[j+1].average_score && j > 0 && students[j].average_score == students[j-1].average_score)) {
Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
这样,就可以按照要求输出学生的基本信息了。
阅读全文