题解 | #成绩排序#

成绩排序

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

#include "stdio.h"

#include "algorithm"

#include "string"

using namespace std;

struct Student{

    char name[20];

    int grade;

    int count;//录入顺序

};

bool comp1(Student lhs,Student rhs){//降序

        if (lhs.grade>rhs.grade)

            return true;

        else if (lhs.grade==rhs.grade && lhs.count<rhs.count)

            return true;

        else

            return false;

}

bool comp2(Student lhs,Student rhs){//升序

    if (lhs.grade<rhs.grade)

        return true;

    else if (lhs.grade==rhs.grade && lhs.count<rhs.count)

        return true;

    else

        return false;

}

int main(){

    int n;

    int flag;//flag:0为降序,1为升序

    Student stu[1000];

    while (scanf("%d %d",&n,&flag)!=EOF){

        for (int i = 0; i < n; ++i) {

            scanf("%s %d",stu[i].name,&stu[i].grade);

            stu[i].count=i;

        }

        if(flag==0)

            sort(stu,stu+n, comp1);

        else

            sort(stu,stu+n, comp2);

        for (int i = 0; i < n; ++i) {

            printf("%s %d\n",stu[i].name,stu[i].grade);

        }

    }

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 12:04
毕业生招你惹你了,问一个发薪日来一句别看网上乱七八糟的你看哪个工作没有固定发薪日扭头就取消了面试就问了一句公司都是这个态度吗还搞上人身攻击了...
程序员小白条:呃呃呃,都还没面试,我都不会问这么细,何况通不通过,去不去都另说,你没实力和学历的话,在外面就这样,说实话没直接已读不回就不错了,浪费时间基本上
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-10 14:10
啊啊啊啊好幸福,妈妈是我找工作发疯前的一束光
榕城小榕树:你是我见过最幸福的牛客男孩
点赞 评论 收藏
分享
frutiger:逆天,我家就安阳的,这hr咋能说3k的,你送外卖不比这工资高得多?还说大厂来的6k,打发叫花子的呢?这hr是怎么做到说昧良心的话的
找工作时遇到的神仙HR
点赞 评论 收藏
分享
05-22 09:23
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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