题解 | #成绩排序#

成绩排序

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

全部评论

相关推荐

05-07 19:10
已编辑
中国科学技术大学 C++
silly01:现在先去 momenta,8-9月去鹅找日常实习,八股文算法背好了你这随便进。不过建议补充一下后端知识,MySQL、Redis看下八股,再补个6824,加点go后台的技术栈,9月随便进大厂。CPP后端只能来WXG
点赞 评论 收藏
分享
frutiger:逆天,我家就安阳的,这hr咋能说3k的,你送外卖不比这工资高得多?还说大厂来的6k,打发叫花子的呢?这hr是怎么做到说昧良心的话的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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