美文网首页
成绩排序

成绩排序

作者: 李清依 | 来源:发表于2018-04-21 16:56 被阅读0次

    题目链接在此
    题目描述
    用一维数组存储学号和成绩,然后,按成绩排序输出。
    输入描述:
    输入第一行包括一个整数N(1<=N<=100),代表学生的个数。
    接下来的N行每行包括两个整数p和q,分别代表每个学生的学号和成绩。
    输出描述:
    按照学生的成绩从小到大进行排序,并将排序后的学生信息打印出来。
    如果学生的成绩相同,则按照学号的大小进行从小到大排序。
    示例1
    输入
    3
    1 90
    2 87
    3 92
    输出
    2 87
    1 90
    3 92
    思路:冒泡排序,简单点就用stl
    其中:

    void swap(struct student *s,int j)
    {
         struct student t;
        t=s[j];
        s[j]=s[j+1];
        s[j+1]=t;
    }
    //交换结构体变量应最好这样写
    

    AC代码:

    #include "stdio.h"
     struct student
    {
        int id;
        int score;
    }stu[100];
    
    
    void swap(struct student *s,int j)
    {
         struct student t;
        t=s[j];
        s[j]=s[j+1];
        s[j+1]=t;
    }
    int main(){
        int n,i;
        scanf("%d",&n);
        for ( i=0;i<n;i++)
        {
            scanf("%d %d",&stu[i].id,&stu[i].score);    
        }
        for (i=n-1;i>=0;i--)
        {
            for (int j=0;j<i;j++)
            {
                if (stu[j].score>stu[j+1].score)
                {
                    swap(stu,j);
                }
                else if (stu[j].score==stu[j+1].score&&stu[j].id>stu[j+1].id)
                {
                    swap(stu,j);
                }
            }
        }
        for(i=0;i<n;i++)
            {
                printf("%d %d\n",stu[i].id,stu[i].score);
            }
    
    }
    

    相关文章

      网友评论

          本文标题:成绩排序

          本文链接:https://www.haomeiwen.com/subject/yqoxlftx.html