题解 | #成绩排序#

成绩排序

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define len 1009
#define maxint 1<<31-1

typedef struct student{
    int num;
    int grade;
}stu;

stu initial_stu(int num,int grade){
    stu st;
    st.num = num;
    st.grade = grade;
    return st;
}

stu stu_list[len];

int cmp(const void*s1,const void*s2){
    stu a1 = *(stu*)s1;
    stu a2 = *(stu*)s2;
    if(a1.grade==a2.grade){
        return a1.num-a2.num;
    }
    return a1.grade-a2.grade;
}

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int num;
        int grade;
        for(int i = 0;i<n;i++){
            scanf("%d %d",&num,&grade);
            stu_list[i] = initial_stu(num,grade);
        }
        qsort(stu_list,n,sizeof(stu),cmp);
        for(int i = 0;i<n;i++){
            stu st = stu_list[i];
            int num = st.num;
            int grade = st.grade;
            printf("%d %d\n",num,grade);
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务