m0_64391767 2022-01-10 16:18 采纳率: 88.9%
浏览 56
已结题

数据结构,输入三个学生成绩,并对其按成绩高低排序

无法对数据进行排序

#include <stdio.h>
#include <stdlib.h>
typedef struct Student{
    int num;
    char name[20];
    char sex;
    float score;
};
void sort(Student a[],int n)
{
    int i,j;
    Student temp;
    for(i=0;i<n-1;i++)
        for(j=0;j<n-1-1;j++)
            if(a[j].score<a[j+1].score)
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
}
int main()
{
    Student stu[3];
    int i;
    for(i=0;i<3;i++)
        scanf("%d%s%*c%c%f",&stu[i].num,&stu[i].name,&stu[i].sex,&stu[i].score);
    sort(stu,3); 
    for(i=0;i<3;i++)
        printf("%d %s %c %f\n",stu[i].num,stu[i].name,stu[i].sex,stu[i].score);
        
    return 0;
}

![img](

https://img-mid.csdnimg.cn/release/static/image/mid/ask/825316208146153.png "#left")

  • 写回答

2条回答 默认 最新

  • bekote 2022-01-10 16:54
    关注
    
    void sort(Student a[],int n)
    {
        int i,j;
        Student temp;
        for(i=0;i<n-1;i++)
        //n-1-1改成n-i-1
            for(j=0;j<n-i-1;j++)
                if(a[j].score<a[j+1].score)
                {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月18日
  • 已采纳回答 1月10日
  • 创建了问题 1月10日