结构体排序

成绩排序

http://www.nowcoder.com/questionTerminal/8e400fd9905747e4acc2aeed7240978b

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

struct stu{
    string name;
    int score;
}student[200];


bool cmp1(stu a,stu b){
    return a.score < b.score;
}
bool cmp2(stu a,stu b){
    return a.score > b.score;
}
int main()
{
    int num,flag;
    while(cin >> num >> flag){

        for(int i = 0;i < num;i ++){
            cin >> student[i].name >> student[i].score;
        }
        if(flag) stable_sort(student,student + num,cmp1);
        else stable_sort(student  ,student + num,cmp2);
        for(int j = 0;j < num;j ++){
            cout << student[j].name << " " <<  student[j].score << endl;
        }
        }
    return 0;
}



全部评论
结构体,c语言的影子
点赞 回复 分享
发布于 2021-03-28 08:41

相关推荐

评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务