PAT_甲级_1062 Talent and Virtue (25分) (C++)【签到题/结构体排序】

目录

1,题目描述

题目大意

2,思路

3,AC代码


1,题目描述

 

Sample Input:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

 

Sample Output:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

题目大意

根据每个人的Virtue_Grade和Talent_Grade来排序。

        if(n.tGrade >= H && n.vGrade >= H)
            n.type = 1;                 //sage
        else if(n.tGrade < H && n.vGrade >= H)
            n.type = 2;                 //nobleman
        else if(n.tGrade < H && n.vGrade < H && n.tGrade <= n.vGrade)
            n.type = 3;                 //fool man
        else
            n.type = 4;                 //small man

 

 

2,思路

(不得不说,签到题虽然没什么营养,但做起来就是爽!)

  • 接收数据时,不合格的pass掉,统计总分,将每个人都做上标记,表明为哪一类人(越优秀,type值越小);
  • 然后就是设计排序函数了:
    bool cmp1(node a, node b){
        if(a.type != b.type){
            return a.type < b.type;
        }else{
            if(a.total != b.total)
                return a.total > b.total;
            else{
                if(a.vGrade != b.vGrade)
                    return a.vGrade > b.vGrade;
                else
                    return a.id < b.id;
            }
        }
    }

     

3,AC代码

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

struct node{
    int id, vGrade, tGrade, total, type;    //total总分 type类型
};
int N, L, H;                                //N人数 L下限 H上限

bool out(node a){                           //不合格
    return (a.tGrade < L || a.vGrade < L);
}
bool cmp1(node a, node b){
    if(a.type != b.type){
        return a.type < b.type;
    }else{
        if(a.total != b.total)
            return a.total > b.total;
        else{
            if(a.vGrade != b.vGrade)
                return a.vGrade > b.vGrade;
            else
                return a.id < b.id;
        }
    }
}

int main(){
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE

    cin>>N>>L>>H;

    vector<node> data;
    node n;
    for(int i = 0; i < N; i++){
        scanf("%d %d %d", &n.id, &n.vGrade, &n.tGrade);
        if(out(n)) continue;            //不合格
        n.total = n.tGrade + n.vGrade;

        if(n.tGrade >= H && n.vGrade >= H)
            n.type = 1;                 //sage
        else if(n.tGrade < H && n.vGrade >= H)
            n.type = 2;                 //nobleman
        else if(n.tGrade < H && n.vGrade < H && n.tGrade <= n.vGrade)
            n.type = 3;                 //fool man
        else
            n.type = 4;                 //small man

        data.push_back(n);
    }
    sort(data.begin(), data.end(), cmp1);

    cout<<data.size()<<endl;
    for(int i = 0; i < data.size(); i++){
        printf("%08d %d %d\n", data[i].id, data[i].vGrade, data[i].tGrade);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值