题解 | #成绩排序#

成绩排序

http://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
struct Student{
    string name;
    int score;
    int order;
};
bool Compare1(Student a,Student b){
    if(a.score==b.score){
        return a.order<b.order;
    }
    else{
        return a.score<b.score;
    }
}
bool Compare2(Student a,Student b){
    if(a.score==b.score){
        return a.order<b.order;
    }
    else{
        return a.score>b.score;
    }
}
int main(){
    int n;
    bool a;
    while(scanf("%d\n",&n)!=EOF){
        Student array[n];
        cin>>a;
        for(int i=0;i<n;++i){
            cin>>array[i].name>>array[i].score;
            array[i].order=i;
        }
        if(a){
            sort(array,array+n,Compare1);
        }
        if(!a){
            sort(array,array+n,Compare2);
        }
        for(int i=0;i<n;++i){
            cout<<array[i].name<<" "<<array[i].score<<endl;
        }
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
牛客21331815...:像我一投就pass,根本不用焦虑泡池子
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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