题解 | EXCEL排序

EXCEL排序

https://www.nowcoder.com/practice/bf3ec474bb7d410dbb9d5bbcd07a93e5

#include <iostream>
using namespace std;
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
struct student{
    string num;  //学生学号
    string name;  //学生姓名
    int score;  //学生成绩
};

bool comp1(student a,student b){
    return a.num<b.num;  //学号递增
}

bool comp2(student a,student b){
    if(a.name!=b.name){
    return a.name<b.name; } //学号递增
    else{
        return a.num<b.num;
    }
}
bool comp3(student a,student b){
    if(a.score!=b.score){
    return a.score<b.score; } //学号递增
    else{
        return a.num<b.num;
    }
}

int main() {
    int n,c;
    while(cin>>n>>c && n!=0){
        vector<student> vec(n);
        for(int i=0;i<n;i++){
            cin>>vec[i].num>>vec[i].name>>vec[i].score;
        }
        if(c==1){
            sort(vec.begin(),vec.end(),comp1);  //按学号递增排序
        }else if(c==2){
            sort(vec.begin(),vec.end(),comp2); // 按姓名的非递减字典序排序
        }else if(c==3){
            sort(vec.begin(),vec.end(),comp3); // 按姓名的非递减字典序排序
        }
        cout<<"Case:"<<endl;
        for(int i=0;i<n;i++){
            cout<<vec[i].num<<" "<<vec[i].name<<" "<<vec[i].score<<endl;

        }

    }

    return 0;
}

全部评论

相关推荐

03-27 01:58
已编辑
西北工业大学 Java
在平静中度过当下:如果这个bg也简历挂的话可能他们现在不缺人了吧,我也是这两天投的,阿里和快手投的岗都是简历秒挂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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