c语言学生成绩降序排序
时间: 2023-08-19 22:36:10 浏览: 108
以下是C语言代码,可以实现学生成绩的降序排序:
```c
#include <stdio.h>
struct student{
char name[20];
int score;
}stu[5];
int main(){
int i, j;
struct student temp;
// 输入5个学生的姓名和成绩
for(i=0; i<5; i++){
printf("请输入第%d个学生的姓名:", i+1);
scanf("%s", stu[i].name);
printf("请输入第%d个学生的成绩:", i+1);
scanf("%d", &stu[i].score);
}
// 冒泡排序
for(i=0; i<4; i++){
for(j=i+1; j<5; j++){
if(stu[i].score < stu[j].score){
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
// 输出排序后的结果
printf("成绩降序排列如下:\n");
for(i=0; i<5; i++){
printf("%s\t%d\n", stu[i].name, stu[i].score);
}
return 0;
}
```
这段代码中,我们使用了结构体来存储每个学生的姓名和成绩,然后使用冒泡排序的方法将学生按照成绩降序排列,并最终输出排序后的结果。