题解 | #成绩排序#

成绩排序

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



#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

struct Student {
    int number;
    int score;
};

const int MaxSize = 100 + 10;

Student arr[MaxSize];

bool Compare(Student x , Student y){
    if(x.score == y.score)
        return x.number < y.number;
    else
        return x.score < y.score;
    
}

int main(){
    
    int n = 0;
    //while(scanf("%d",&n)!=EOF)
    scanf("%d",&n);
    for(int i = 0; i<n;i++){
        scanf("%d%d",&arr[i].number,&arr[i].score);
    }
    sort(arr, arr + n , Compare);
    for(int i = 0; i<n ;i++){
        cout << arr[i].number << " " << arr[i].score << endl;
    }
    return 0;
    
}
//直接从结构体定义中定义大小,此为默认比较方法,不需要compare函数。
struct Student {
    int number;
    int score;
    bool operator< (Student student) const {
        if (score == student.score){
            return number < student.number;
        }else {
            return score < student.score;
        }
    }
}
全部评论

相关推荐

会飞的猿:本人来了,手一抖转错了,我是学生,能还给我吗
点赞 评论 收藏
分享
02-10 12:23
已编辑
新余学院 C++
采集想要offer:专业技能那里要一条一条的列出来吧,感觉你项目很厉害了,但是如果你不写技术栈面试官对你项目不太懂的话都没办法问你八股😂C++都是基架岗,都是一群9✌🏻在卷,我觉得你要是有时间学个go把MySQL和redis写上去找个开发岗吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务