#include <stdio.h>
// 定义学生结构体
struct student {
char name[20];
float score;
};
int main() {
// 声明存储5位学生信息的数组
struct student students[5];
// 输入5位学生的信息
for (int i = 0; i < 5; ++i) {
scanf("%s%f", students[i].name, &students[i].score);
}
// 冒泡排序,按照成绩降序排列
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4 - i; ++j) {
if (students[j].score < students[j + 1].score) {
// 交换位置
struct student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
// 输出排序后的学生信息
for (int i = 0; i < 5; ++i) {
printf("%s %.1f\n", students[i].name, students[i].score);
}
return 0;
}
杭师大c语言课程练习
最新推荐文章于 2024-07-10 21:37:49 发布