题解 | 成绩排序

成绩排序

https://www.nowcoder.com/practice/3f27a0a5a59643a8abf0140b9a8cf1f7

#include <stdio.h>
#include<stdlib.h>
#include <string.h>

#define N 100


typedef struct student
{
    int sno;
    int score;
}STUDENT;

int SortAsc(int a,int b)//判断前后大小,不同结果返回不同的值用于后续判断
{
    if(a>b)
    {
        return 1;
    }
    else if (a==b)
    {
        return 2;
    }
    else
    {
        return 0;
    }
}

void bubble(STUDENT stu[],int SortAsc(int a, int b),int num)//冒泡排序函数
{
    STUDENT temp;
    int flag=0;
    for(int i=0;i<num;i++)
    {
        
        for(int j=num-1;j>i;j--)
        {
            if(SortAsc(stu[j-1].score,stu[j].score)==1)
            {
                flag=1;
                temp=stu[j-1];
                stu[j-1]=stu[j];
                stu[j]=temp;
            }
            else if(SortAsc(stu[j-1].score,stu[j].score)==2)
            {
                if(stu[j-1].sno>stu[j].sno)
                {
                    flag=1;
                    temp=stu[j-1];
                    stu[j-1]=stu[j];
                    stu[j]=temp;
                }
            }
        }
        if(flag==0)
        {
            break;
        }
    }
}

void output(STUDENT stu[],int num)//输出函数
{
    for(int i=0;i<num;i++)
    {
        
        printf("%d %d\n",stu[i].sno,stu[i].score);
    }
}

int main() 
{
    int num;
    while(scanf("%d",&num)!=EOF)
    {
        STUDENT stu[num];
        for(int i=0;i<num;i++)
        {
            scanf("%d",&stu[i].sno);
            scanf("%d",&stu[i].score);
        }
        bubble(stu,SortAsc,num);
        output(stu,num);
    }
    return 0;
}

全部评论

相关推荐

11-11 16:40
已编辑
门头沟学院 人工智能
不知道怎么取名字_:这个有点不合理了,相当于已经毕业了,但还是没转正,这不就是白嫖
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务