题解 | #成绩排序#

成绩排序题目链接

自定义比较器

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
typedef struct{
    int ID;
    int goal;
}student;
bool cmp(const student s1,const student s2){
    // 牢记true是返回前面的
    if(s1.goal<s2.goal) return true;
    else if(s1.goal > s2.goal) return false;
    else if(s1.ID < s2.ID) return true;
    else return false;
}
int main(){
    int n;
    cin>>n;
    vector<student> st;
    int p,q = 0;
    while(cin>>p&&cin>>q){
        student s;
        s.ID = p;
        s.goal = q;
        st.push_back(s);
    }
    sort(st.begin(),st.begin()+st.size(),cmp);
    for(student item : st){
        cout<<item.ID<<" "<<item.goal<<" \n";
    }
}
全部评论

相关推荐

刘湘_passion:太强了牛肉哥有被激励到
点赞 评论 收藏
分享
03-10 14:19
已编辑
重庆邮电大学 前端工程师
球Offer上岸👑:测试也难求一面 逆天
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务