题解 | #成绩排序#

stable_sort 稳定排序

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct{
    string name;
    int goal;
    int num;
}student;
bool cmp1(student s1,student s2){
//     if(s1.goal<s2.goal) return true;
//     else if(s1.goal==s2.goal) return true;
//     else return false;
    return s1.goal<s2.goal;
}
bool cmp2(student s1,student s2){
//     if(s1.goal>s2.goal) return true;
//     else if(s1.goal==s2.goal) return true;
//     else return false;
    return s1.goal>s2.goal;
}
int main(){
    int n;
    int tag;
    while(cin>>n&&cin>>tag){
        vector<student> st;
        string name; int goal;
        for(int i=0;i<n;i++){
            cin>>name>>goal;
            student s;
            s.name = name;
            s.goal = goal;
            st.push_back(s);
        }
        if(tag == 1){
            stable_sort(st.begin(),st.begin()+st.size(),cmp1);
        }else if(tag == 0){
            stable_sort(st.begin(),st.begin()+st.size(),cmp2);
        }
        for(student item: st){
            cout<<item.name<<" "<<item.goal<<"\n";
        }
    
    }
    
}
全部评论

相关推荐

04-25 19:29
已编辑
宁波大学 运营
被普调的六边形战士很高大:你我美牛孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务