前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PAT A1062

PAT A1062

作者头像
全栈程序员站长
发布2021-07-01 10:31:46
1570
发布2021-07-01 10:31:46
举报
文章被收录于专栏:全栈程序员必看
clipboard.png
clipboard.png

含简单的一道排序题,主要注意一下cmp和sort组合使用; 之前一直搞不太清楚cmp的返回条件,所以这里做一个总结; cmp自定义函数可以多种组合判定,就像本题的判定类似;

clipboard.png
clipboard.png

如上图所示,唯一要注意的是return返回值。其实大可不用过于死板的背true或者false的意义,只需要看比较元素的关系;如果想要升序序列,只需要a的值小于b的值,降序序列同理; 详细代码如下所示:

代码语言:javascript
复制
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
using std::vector;
struct student{
    char id[10];
    int de,cai,sum;
    int flag;
}stu[100010];
bool cmp(student a,student b){
    if(a.flag!=b.flag){
        return a.flag<b.flag;
    }else if(a.sum!=b.sum){
        return a.sum>b.sum;
    }else if(a.de!=b.de){
        return a.de>b.de;
    }else{
        return strcmp(a.id,b.id)<0;
    }
}
int N,L,H;
int main(){
    scanf("%d%d%d",&N,&L,&H);
    int number,t,v;
    int m=N;
    for(int i=0;i<N;i++){
        scanf("%s%d%d",&stu[i].id,&stu[i].de,&stu[i].cai);
        stu[i].sum=stu[i].de+stu[i].cai;
        if(stu[i].de<L||stu[i].cai<L){
            stu[i].flag=5;
            m--;
        }else if(stu[i].de>=H&&stu[i].cai>=H)
            stu[i].flag=1;
        else if(stu[i].de>=H&&stu[i].cai<H)
            stu[i].flag=2;
        else if(stu[i].de>=stu[i].cai)
            stu[i].flag=3;
        else
            stu[i].flag=4;
    }
    sort(stu,stu+N,cmp);
    printf("%d\n",m);
    for(int i=0;i<m;i++){
        printf("%s %d %d\n",stu[i].id,stu[i].de,stu[i].cai);
    } 
    system("pause");
    return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/101135.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档