题解 | #成绩排序#

成绩排序

https://www.nowcoder.com/practice/0383714a1bb749499050d2e0610418b1

#include <array>
#include<iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxx = 10000;

struct Student{
    string name;
    int grade;
    int sit;
};
Student arr[maxx];
bool comp(Student lhs,Student rhs)
{
        if(lhs.grade < rhs.grade){
            return true;
        }
        else if(lhs.grade == rhs.grade && lhs.sit < rhs.sit) {
            return true;
        }
        else{
            return false;
        }
}
bool comp1(Student lhs,Student rhs){
        if(lhs.grade > rhs.grade)
        {
            return true;
        }
        else if (lhs.grade == rhs.grade && lhs.sit < rhs.sit) {
            return true;
        }
        else return false;
}
int main() {
    int N;
    int order;
    while (scanf("%d%d",&N,&order) != EOF) {
        for(int i = 0;i < N ; ++i){
            cin >> arr[i].name >> arr[i].grade;
            arr[i].sit = i;
        }
        if(1 == order)
        {
            sort(arr,arr+N,comp);
        }
        if (0 == order) {
            sort(arr,arr+N,comp1);
        }
        for(int i = 0;i < N ; ++i)
        {
            cout << arr[i].name << " " << arr[i].grade <<endl;
        }
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
投了多少份简历才上岸
点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 14:00
不想多说了,什么逆天HR,还要教我礼貌😂
机械打工仔:这不纯傻卵吗,他还操心上别人老板了
投递BOSS直聘等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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