为了账号安全,请及时绑定邮箱和手机立即绑定

我的排序出问题了,不知道哪错了!

我的排序出问题了,不知道哪错了!

C
慕少7147203 2017-12-14 15:09:37
#include <stdio.h> #include <string.h> #define M 1701 // 学号 #define N 10 struct student // 结构体 { int num; // 学号 char name[N]; // 姓名 float score; // 成绩 } stu1[N]; void main() { int i; printf("请依次输入学生的成绩,姓名:\n"); for (i = 0; i < N; i++) { stu1[i].num = M + i; // 学号赋值 printf("%d\t", stu1[i].num); // 打印学号 scanf("%f%s", &stu1[i].score, stu1[i].name); // 输入成绩,姓名 } printf("下面按成绩大小排序,按任意键继续\n"); getchar(); // 任意键继续 for (i = 0; i < N - 1; i++) // 冒泡排序 { int max = i; for (int j = i + 1; j < N; j++) { if (stu1[j].score > stu1[max].score) max = j; struct student t; // 引入一个结构体 memcpy(&t, &stu1[max], sizeof(stu1)); memcpy(&stu1[max], &stu1[i], sizeof(stu1)); memcpy(&stu1[i], &t, sizeof(stu1)); } printf("%d\t%s\t%f\n", stu1[i].num, stu1[i].name, stu1[i].score); // 打印排序 } }
查看完整描述

1 回答

?
慕沐4487107

TA贡献11条经验 获得超1个赞

冒泡法中if后的语句出错,应该是实现两个变量交换的,你却直接赋值

查看完整回答
反对 回复 2017-12-15
  • 1 回答
  • 0 关注
  • 850 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信