题解 | #成绩排序#

成绩排序

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;
        }
    }
}
全部评论

相关推荐

07-01 17:14
中北大学 Java
兄弟们是真是假
牛客46374834...:我在boss上投java岗从来没成功过
点赞 评论 收藏
分享
那一天的Java_J...:他本来公司就是做这个的,不就是正常的游戏客户端和服务器开发,软硬件联动,有啥恶心不恶心的,提前告诉你就是怕你接受不了,接受不了就没必要再往后走流程浪费时间,虽然这公司是一坨。
点赞 评论 收藏
分享
07-02 13:52
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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