A1062 Talent and Virtue (25 分| 排序,附详细注释,逻辑分析)

写在前面
  • 思路分析
    • 结构体数组封装学生元信息
      • 结构体数组vector v[4]封装4类考生
    • cmp函数,较耗费时间
      • 排序先按照总分排序,然后按照德分排序,最后按照才分排序
    • 输出符合条件的结果~
  • 题目简单,20分钟a题
测试用例
  • 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
    
    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
    
ac代码
  • #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    struct node
    {
        int num, de, cai;
    };
    
    int cmp(node a,node b)
    {
        if((a.de + a.cai) != (b.de+b.cai))
            return (a.de + a.cai) > (b.de+b.cai);
        else if(a.de != b.de)
            return a.de>b.de;
        else
            return a.num<b.num;
    }
    
    int main()
    {
        int n, low, high;
        scanf("%d %d %d", &n, &low, &high);
        vector<node> v[4];
        node tmp;
        int total = n;
    
        for(int i=0; i<n; i++)
        {
            scanf("%d %d %d", &tmp.num,&tmp.de,&tmp.cai);
            if(tmp.de < low || tmp.cai < low)
                total--;
            else if(tmp.de >= high && tmp.cai >= high)
                v[0].push_back(tmp);
            else if(tmp.de >= high && tmp.cai<high)
                v[1].push_back(tmp);
            else if(tmp.de < high && tmp.cai < high && tmp.de >= tmp.cai)
                v[2].push_back(tmp);
            else
                v[3].push_back(tmp);
        }
    
        printf("%d\n", total);
        for(int i=0; i<4; i++)
        {
            sort(v[i].begin(), v[i].end(), cmp);
            for(int j=0; j<v[i].size(); j++)
                printf("%d %d %d\n", v[i][j].num, v[i][j].de, v[i][j].cai);
        }
    
        return 0;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xsimah

创作不易,感谢客官的打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值