鱼C论坛

 找回密码
 立即注册
查看: 1556|回复: 2

[已解决]结构体编译有些问题

[复制链接]
发表于 2023-11-4 22:07:40 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. struct Student{
  3.         char name[21];
  4.         int score;
  5. };
  6. int main(){
  7.         struct Student a[100];
  8.         int n,temp;
  9.         scanf("%d",&n);//接下来输入读取人数
  10.         for(int i=0;i<n;i++){
  11.                 scanf("%s %d",a[i].name,&a[i].score);       
  12.         }
  13.         //冒泡数组排列核心
  14.         for(int i=0;i<n-1;i++){//n个数比较n-1趟
  15.             for(int j=0;j<n-i;j++){
  16.                         if(a[j].score>a[j+1].score){
  17.                                 temp=a[j];
  18.                                 a[j]=a[j+1];
  19.                                 a[j+1]=temp;
  20.                         }
  21.                 }
  22.         }
  23.         //输出
  24.         for(int i=0;i<n;i++){
  25.                 printf("%s\n",a[i].name);
  26.         }
  27.         return 0;
  28. }
复制代码
最佳答案
2023-11-4 22:08:55
在这份代码中,存在以下几个问题:

1. 在冒泡排序的核心部分,变量temp是一个int类型的变量,但在将结构体数组中的元素进行交换时,需要交换整个结构体对象。因此,需要将变量temp的类型改为Student类型。

2. 冒泡排序的循环条件存在问题。外层循环应该从0到n-2,因为最后一趟只剩下最后一个元素,不需要再比较了。内层循环应该从0到n-i-2,因为前面已经排好序的i个元素不需要再比较了。

修改后的代码如下所示:
  1. ++
  2. #include<stdio.h>
  3. struct Student{
  4.     char name[21];
  5.     int score;
  6. };
  7. int main(){
  8.     struct Student a[100];
  9.     int n;
  10.     scanf("%d",&n);//接下来输入读取人数
  11.     for(int i=0;i<n;i++){
  12.         scanf("%s %d",a[i].name,&a[i].score);
  13.     }
  14.     //冒泡数组排列核心
  15.     for(int i=0;i<n-1;i++){//n个数比较n-1趟
  16.         for(int j=0;j<n-i-1;j++){ // 修正内层循环的上限
  17.             if(a[j].score>a[j+1].score){
  18.                 struct Student temp = a[j]; // 修正交换操作
  19.                 a[j] = a[j+1];
  20.                 a[j+1] = temp;
  21.             }
  22.         }
  23.     }
  24.     //输出
  25.     for(int i=0;i<n;i++){
  26.         printf("%s\n",a[i].name);
  27.     }
  28.     return 0;
  29. }
复制代码

经过修改后的代码,可以正确地对结构体数组进行冒泡排序,并输出结果。希望对你有帮助!

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-4 22:08:36 | 显示全部楼层
问题的原因是在冒泡排序部分,将结构体数组中的元素进行交换时出现了问题。在代码中,结构体数组a的元素类型是struct Student,而temp的类型是int。在进行交换时,应该将a[j]和a[j+1]两个结构体对象进行交换,而不是直接将它们的score成员进行交换。

正确的交换代码应该是这样的:

  1. temp = a[j];
  2. a[j] = a[j+1];
  3. a[j+1] = temp;
复制代码


修改后的代码如下:

  1. #include <stdio.h>
  2. struct Student{
  3.     char name[21];
  4.     int score;
  5. };
  6. int main(){
  7.     struct Student a[100];
  8.     int n,temp;
  9.     scanf("%d",&n);//接下来输入读取人数
  10.     for(int i=0;i<n;i++){
  11.         scanf("%s %d",a[ i].name,&a[ i].score);
  12.     }
  13.     //冒泡数组排列核心
  14.     for(int i=0;i<n-1;i++){
  15.         for(int j=0;j<n-i-1;j++){
  16.             if(a[j].score>a[j+1].score){
  17.                 temp=a[j];
  18.                 a[j]=a[j+1];
  19.                 a[j+1]=temp;
  20.             }
  21.         }
  22.     }
  23.     //输出
  24.     for(int i=0;i<n;i++){
  25.         printf("%s\n",a[ i].name);
  26.     }
  27.     return 0;
  28. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-4 22:08:55 | 显示全部楼层    本楼为最佳答案   
在这份代码中,存在以下几个问题:

1. 在冒泡排序的核心部分,变量temp是一个int类型的变量,但在将结构体数组中的元素进行交换时,需要交换整个结构体对象。因此,需要将变量temp的类型改为Student类型。

2. 冒泡排序的循环条件存在问题。外层循环应该从0到n-2,因为最后一趟只剩下最后一个元素,不需要再比较了。内层循环应该从0到n-i-2,因为前面已经排好序的i个元素不需要再比较了。

修改后的代码如下所示:
  1. ++
  2. #include<stdio.h>
  3. struct Student{
  4.     char name[21];
  5.     int score;
  6. };
  7. int main(){
  8.     struct Student a[100];
  9.     int n;
  10.     scanf("%d",&n);//接下来输入读取人数
  11.     for(int i=0;i<n;i++){
  12.         scanf("%s %d",a[i].name,&a[i].score);
  13.     }
  14.     //冒泡数组排列核心
  15.     for(int i=0;i<n-1;i++){//n个数比较n-1趟
  16.         for(int j=0;j<n-i-1;j++){ // 修正内层循环的上限
  17.             if(a[j].score>a[j+1].score){
  18.                 struct Student temp = a[j]; // 修正交换操作
  19.                 a[j] = a[j+1];
  20.                 a[j+1] = temp;
  21.             }
  22.         }
  23.     }
  24.     //输出
  25.     for(int i=0;i<n;i++){
  26.         printf("%s\n",a[i].name);
  27.     }
  28.     return 0;
  29. }
复制代码

经过修改后的代码,可以正确地对结构体数组进行冒泡排序,并输出结果。希望对你有帮助!

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 01:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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