题解 | 成绩排序

成绩排序

https://www.nowcoder.com/practice/7a2f7d304d9e43b1bb2a6e72ed65bf51

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct student {
    string name;
    int age;
    int score;
    student(string name,int age,int score):name(name),age(age),score(score){}
    bool operator < (const student& S)const {
        if (score != S.score)return score < S.score;
        else {
            if (name != S.name)return name < S.name;
            else {
                return age < S.age;
            }
        }
    }
};

int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        vector<student>s;
        for(int i=0;i<n;i++){
            string name;
            int age,score;
            cin>>name>>age>>score;
            s.push_back( student(name,age,score));
        }
        sort(s.begin(),s.end());
        for(int i=0;i<s.size();i++){
            printf("%s %d %d\n",s[i].name.c_str(),s[i].age,s[i].score);
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

03-20 18:39
已编辑
电子科技大学 C++
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务