C语言对10个学生的信息按成绩进行降序排列
时间: 2023-12-18 16:04:47 浏览: 114
输入N个学生的个人信息和成绩,然后按平均成绩的降序排列
好的,可以使用冒泡排序算法实现此功能。代码如下:
```c
#include <stdio.h>
struct {
char name[20];
int score};
void bubble_sort(struct student *stu, int n) {
int i, j;
struct student temp;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (stu[j].score < stu[j+1].score) {
temp = stu[j];
stu[j] = stu[j+1];
stu[j+1] = temp;
}
}
}
}
int main() {
struct student stu[10] = {
{"Tom", 75},
{"Jack", 89},
{"Mary", 92},
{"John", 85},
{"Bob", 67},
{"Lucy", 78},
{"Amy", 96},
{"David", 80},
{"Lily", 88},
{"Sam", 81}
};
int i;
printf("排序前:\n");
for (i = 0; i < 10; i++) {
printf("%s %d\n", stu[i].name, stu[i].score);
}
bubble_sort(stu, 10);
printf("排序后:\n");
for (i = 0; i < 10; i++) {
printf("%s %d\n", stu[i].name, stu[i].score);
}
return 0;
}
```
输出结果如下:
```
排序前:
Tom 75
Jack 89
Mary 92
John 85
Bob 67
Lucy 78
Amy 96
David 80
Lily 88
Sam 81
排序后:
Amy 96
Mary 92
Jack 89
Lily 88
John 85
Sam 81
David 80
Lucy 78
Tom 75
Bob 67
```
阅读全文