B1004. 成绩排名

这一题总算是把C++的重载活学活用了一回,节省了很多脑细胞。

#include<bits/stdc++.h>
using namespace std;

struct student{
    string name;
    string code;
    int score;
    //operator <
    bool operator < (const student& b){
        return this->score<b.score;
    }
};
vector<student> students;
void solve(){
    int n;
    cin>>n;
    while(n--){
        student s;
        cin>>s.name>>s.code>>s.score;
        students.push_back(s);
    }
    sort(students.begin(),students.end());
    cout<<students.back().name<<" "<<students.back().code<<endl;
    cout<<students.front().name<<" "<<students.front().code<<endl;
}
int main(){
    solve();
    return 0;
}

sort一把梭,用vector存结构体,非常省事。

keep going
原文地址:https://www.cnblogs.com/MarkKobs-blog/p/10550423.html