题解 | 成绩排序

成绩排序

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")

全部评论

相关推荐

07-02 22:46
门头沟学院 Java
码农索隆:hr:“管你投没投,先挂了再说”
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 15:58
投个小米提前批试试水,先投一个岗位看看形势,不行就再沉淀一下投第二个岗位,莫辜负
Java抽象带篮子:我嘞个骚刚,已经开始研发6g了吗
投递小米集团等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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