指针冒泡算法====学生成绩的排序

 

学生成绩的排序

 

 

好的,我会为您解答。首先,我们需要定义一个结构体,用于存储学生的姓名和成绩,如下所示: ```c struct student { char name[20]; int score; }; ``` 然后,我们可以创建一个结构体指针数组,用于存储多个学生的信息,如下所示: ```c struct student *stu[5]; ``` 接下来,我们可以使用循环语句来输入每个学生的姓名和成绩,如下所示: ```c for (int i = 0; i < 5; i++) { stu[i] = (struct student*)malloc(sizeof(struct student)); // 动态分配内存 printf("请输入第%d个学生的姓名和成绩:", i+1); scanf("%s %d", stu[i]->name, &stu[i]->score); } ``` 然后,我们可以使用冒泡排序算法,按照学生的成绩从高到低排序,如下所示: ```c for (int i = 0; i < 4; i++) { for (int j = 0; j < 4-i; j++) { if (stu[j]->score < stu[j+1]->score) { struct student *temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } } ``` 最后,我们可以使用循环语句输出已排序的学生信息,如下所示: ```c printf("按成绩从高到低排序结果如下:\n"); for (int i = 0; i < 5; i++) { printf("姓名:%s,成绩:%d\n", stu[i]->name, stu[i]->score); } ``` 完整代码如下所示: ```c #include <stdio.h> #include <stdlib.h> struct student { char name[20]; int score; }; int main() { struct student *stu[5]; for (int i = 0; i < 5; i++) { stu[i] = (struct student*)malloc(sizeof(struct student)); printf("请输入第%d个学生的姓名和成绩:", i+1); scanf("%s %d", stu[i]->name, &stu[i]->score); } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4-i; j++) { if (stu[j]->score < stu[j+1]->score) { struct student *temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; } } } printf("按成绩从高到低排序结果如下:\n"); for (int i = 0; i < 5; i++) { printf("姓名:%s,成绩:%d\n", stu[i]->name, stu[i]->score); } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值