PAT-1062 Talent and Virtue

1062 Talent and Virtue

part 2, 2.1

自己解法

  • 认真读题!(惨痛教训)
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

class person
{
public:
    person(int ID, int V, int T, int H)
    {
        this->ID = ID;
        this->V = V;
        this->T = T;
        this->sum = V + T;
        if (V >= H && T >= H)
            this->flag = 1;
        else if (V >= H && T < H)
            this->flag = 2;
        else if (V < H && V >= T)
            this->flag = 3;
        else
            this->flag = 4;
    }

    int ID;
    int V, T;
    int sum, flag; // flag = 1 是 sages(V>=H,T>=H),flag = 2 是 nobleman(V>=H,T<H), flag = 3 是 foolman(T=<V<H)
};

bool cmp(person a, person b)
{
    return a.flag != b.flag ? a.flag < b.flag : (a.sum != b.sum ? a.sum > b.sum : (a.V != b.V ? a.V > b.V : a.ID < b.ID));
}

int main()
{
    int N, L, H;
    cin >> N >> L >> H;
    vector<person> v;
    for (int i = 0; i < N; i++)
    {
        int ID, V, T;
        cin >> ID >> V >> T;
        if (V >= L && T >= L)
        {
            person p(ID, V, T, H);
            v.push_back(p);
        }
    }
    sort(v.begin(), v.end(), cmp);
    int v_size = v.size();
    cout << v_size << endl;
    for (int i = 0; i < v_size; i++)
        cout << v[i].ID << " " << v[i].V << " " << v[i].T << endl;

    system("pause");
    return 0;
}

大神解法

  • 柳神
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node {
    int num, de, cai;
};
int cmp(struct node a, struct 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 temp;
    int total = n;
    for (int i = 0; i < n; i++) {
        scanf("%d %d %d", &temp.num, &temp.de, &temp.cai);
        if (temp.de < low || temp.cai < low)
            total--;
        else if (temp.de >= high && temp.cai >= high)
            v[0].push_back(temp);
        else if (temp.de >= high && temp.cai < high)
            v[1].push_back(temp);
        else if (temp.de < high && temp.cai < high && temp.de >= temp.cai)
            v[2].push_back(temp);
        else
            v[3].push_back(temp);
    }
    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;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值