题解 | #成绩排序#

成绩排序

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;
}

全部评论

相关推荐

一天代码十万三:实习东西太少了,而且体现不出你业务,3个月不可能就这点产出吧,建议实习多写点,玩具项目面试官都不感兴趣的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务