鱼C论坛

 找回密码
 立即注册
查看: 2444|回复: 7

C语言

[复制链接]
发表于 2020-4-8 17:32:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
题目要求:输入n(n<50)个学生的成绩信息,按照学生的个人平均成绩从高到低输出他们的信息。



我这段代码好像有点问题,但是找不出来哪里有问题?大佬指点迷津。


  1. #include<stdio.h>

  2. struct student
  3. {
  4.     int num;
  5.     char name[10];
  6.     int computer,english,math;
  7.     double average;
  8. };

  9. int main()
  10. {
  11.     struct student students[50],temp;
  12.     int n,j,i,index;

  13.     printf("请输入你要输入的学生个数:");
  14.     scanf("%d",&n);

  15.     for(i = 0; i < n; i++)
  16.     {
  17.         printf("Input the info of No.%d:",i+1);
  18.         scanf("%d",&students[i].num);

  19.         printf("Input the name of student %d:",i+1);
  20.         scanf("%s",students[i].name);

  21.         printf("Input math score:");
  22.         scanf("%d",&students[i].math);

  23.         printf("Input english score:");
  24.         scanf("%d",&students[i].english);

  25.         printf("Input computer score:");
  26.         scanf("%d",&students[i].computer);

  27.         students[i].average = (students[i].math + students[i].english + students[i].computer) / 3.0;
  28.     }


  29.     for(i = 0; i < n-1; i++)
  30.     {
  31.         for(j = i+1; j < n; j++)
  32.         {
  33.             if(students[j].average > students[i].average)
  34.             {
  35.                 index = j;
  36.             }
  37.         }
  38.         temp = students[j];
  39.         students[j] = students[i];
  40.         students[i] = temp;
  41.     }


  42.     printf("num\tname\taverage\t");
  43.     printf("\n");
  44.     for(i = 0; i < n; i++)
  45.     {
  46.         printf("%d\t%s\t%.2lf\n",students[i].num,students[i].name,students[i].average);
  47.     }
  48. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-8 17:46:54 | 显示全部楼层
  1. 请输入你要输入的学生个数:4
  2. Input the info of No.1:1
  3. Input the name of student 1:A
  4. Input math score:11
  5. Input english score:22
  6. Input computer score:33
  7. Input the info of No.2:2
  8. Input the name of student 2:B
  9. Input math score:12
  10. Input english score:23
  11. Input computer score:31
  12. Input the info of No.3:3
  13. Input the name of student 3:C
  14. Input math score:13
  15. Input english score:21
  16. Input computer score:32
  17. Input the info of No.4:4
  18. Input the name of student 4:D
  19. Input math score:33
  20. Input english score:22
  21. Input computer score:11
  22. num     name    average
  23. -858993460      烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫   -92559631349317831000000000000000000000000000000000000000000000.00
  24. 1       A       22.00
  25. 2       B       22.00
  26. 4       D       22.00
  27. Press any key to continue
复制代码

经典烫烫烫(找不出错)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 19:52:50 | 显示全部楼层
本帖最后由 sunrise085 于 2020-4-8 19:59 编辑

程序第49~51行,交换两个student的时候写错了
交换的应该是i和index,你写成交换i和j了
另外,不管在一次内循环中,有没有找到比当前student[i]大的值,你都会进行交换,这是不对的

  1. #include<stdio.h>

  2. struct student
  3. {
  4.     int num;
  5.     char name[10];
  6.     int computer,english,math;
  7.     double average;
  8. };

  9. int main()
  10. {
  11.     struct student students[50],temp;
  12.     int n,j,i,index;

  13.     printf("请输入你要输入的学生个数:");
  14.     scanf("%d",&n);

  15.     for(i = 0; i < n; i++)
  16.     {
  17.         printf("Input the info of No.%d:",i+1);
  18.         scanf("%d",&students[i].num);

  19.         printf("Input the name of student %d:",i+1);
  20.         scanf("%s",students[i].name);

  21.         printf("Input math score:");
  22.         scanf("%d",&students[i].math);

  23.         printf("Input english score:");
  24.         scanf("%d",&students[i].english);

  25.         printf("Input computer score:");
  26.         scanf("%d",&students[i].computer);

  27.         students[i].average = (students[i].math + students[i].english + students[i].computer) / 3.0;
  28.     }


  29.     for(i = 0; i < n-1; i++)
  30.     {
  31.         for(j = i+1; j < n; j++)
  32.         {
  33.             if(students[j].average > students[i].average)
  34.             {
  35.                 temp = students[j];
  36.                 students[j] = students[i];
  37.                 students[i] = temp;
  38.             }
  39.         }
  40.     }


  41.     printf("num\tname\taverage\t");
  42.     printf("\n");
  43.     for(i = 0; i < n; i++)
  44.     {
  45.         printf("%d\t%s\t%.2lf\n",students[i].num,students[i].name,students[i].average);
  46.     }
  47. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 19:57:57 From FishC Mobile | 显示全部楼层
if(students[j].average > students[i].average)
          个人认为,上面这一行,在双层for下,有问题如下:
当第一位学生i时,j为第二位学生。
因为有n-1=49,下标正确实际只能访问到第48元素就跳出这是i层for的问题。
j层问题是由于i层已0元素开始,那么j=i+1
就说明j层是从下标1开始的,首元素没有计算。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 20:08:46 | 显示全部楼层
howzyao 发表于 2020-4-8 19:57
if(students[j].average > students.average)
          个人认为,上面这一行,在双层for下,有问题如下 ...

你的理解是不对的。你该去学习一下冒泡法排序了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-8 22:16:27 From FishC Mobile | 显示全部楼层
我反应过来了,这是一个学生和二个学生做比较。
竟然没有考虑到这是一条线上的50个学生。。。
谢谢你的提醒。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-9 09:49:28 | 显示全部楼层
关键还是对冒泡排序理解的不透彻。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-9 21:03:33 | 显示全部楼层
本帖最后由 老牛来学习 于 2020-4-9 21:16 编辑
sunrise085 发表于 2020-4-8 19:52
程序第49~51行,交换两个student的时候写错了
交换的应该是i和index,你写成交换i和j了
另外,不管在一次 ...

  1.     for(i = 0; i < n-1; i++)
  2.     {
  3.         for(j = i+1; j < n; j++)
  4.         {
  5.             if(students[j].average > students[i].average)
  6.             {
  7.                 index = j;
  8.             }
  9.         }
  10.         temp = students[index];
  11.         students[index] = students[i];
  12.         students[i] = temp;
  13.     }
复制代码

所以改成index之后,这段代码相当于是不管j比不比i大,最后都要交换一次,导致如果某一次循环j不比i大,i将和上一次的index交换。是这样吗?

改成这样是不是就行了?

前面定义一个k = 0
  1.     for(i = 0; i < n-1; i++)
  2.     {
  3.         for(j = i+1; j < n; j++)
  4.         {
  5.             if(students[j].average > students[i].average)
  6.             {
  7.                 k = (index = j);
  8.             }
  9.         }
  10.         if(k)
  11.         {
  12.             temp = students[index];
  13.             students[index] = students[i];
  14.             students[i] = temp;
  15.         }
  16.         k = 0;
  17.     }
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-5 02:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表