[PAT Basic] 1015 德才论 (25 分)

Posted esrevinud

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[PAT Basic] 1015 德才论 (25 分)相关的知识,希望对你有一定的参考价值。

思路

根据题意,首先创建对应结构体,分别定义四个vector,根据提交逐个push, 重点是sort比较函数的逻辑,然后对每个vector排序输出

cpp

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;
int n, l, h, suc = 0;

typedef struct stu {
    int id;
    int quality;
    int knows;
}stu;

bool cmp(stu a, stu b) {
    bool result;
    int all_a = a.quality + a.knows;
    int all_b = b.quality + b.knows;

    if (all_a == all_b) {
        if (a.quality == b.quality) {
            if (a.id < b.id) {
                result = true;
            } else {
                result = false;
            }
        } else {
            if (a.quality > b.quality) {
                result = true;
            } else {
                result = false;
            }
        }
    } else {
        if (all_a > all_b) {
            result = true;
        } else {
            result = false;
        }
    }

    return result;
}

int main() {
    scanf("%d %d %d", &n, &l, &h);
    vector<stu> first;
    vector<stu> second;
    vector<stu> third;
    vector<stu> fourth;
    stu s;
    int tid, def, abc;
    for (int i = 0; i < n; i ++) {
        scanf("%d %d %d", &tid, &def, &abc);
        s.id = tid;
        s.quality = def;
        s.knows = abc;
        if (s.quality >= l && s.knows >= l) {
            suc ++;
            if (s.quality >= h && s.knows >= h) {
                first.push_back(s);
                continue;
            } else if (s.quality >= h && s.knows < h) {
                second.push_back(s);
                continue;
            } else if (s.quality >= s.knows) {
                third.push_back(s);
                continue;
            } else {
                fourth.push_back(s);
                continue;
            }
        }
    }
    sort(first.begin(), first.end(), cmp);
    sort(second.begin(), second.end(), cmp);
    sort(third.begin(), third.end(), cmp);
    sort(fourth.begin(), fourth.end(), cmp);
    printf("%d
", suc);

    for (int i = 0; i < first.size(); i ++) {
        printf("%d %d %d
", first[i].id, first[i].quality, first[i].knows);
    }
    for (int i = 0; i < second.size(); i ++) {
        printf("%d %d %d
", second[i].id, second[i].quality, second[i].knows);
    }
    for (int i = 0; i < third.size(); i ++) {
        printf("%d %d %d
", third[i].id, third[i].quality, third[i].knows);
    }
    for (int i = 0; i < fourth.size(); i ++) {
        printf("%d %d %d", fourth[i].id, fourth[i].quality, fourth[i].knows);
        if (i != fourth.size() - 1) {
            printf("
");
        }
     }

    return 0;
}

以上是关于[PAT Basic] 1015 德才论 (25 分)的主要内容,如果未能解决你的问题,请参考以下文章

PAT1015 德才论 (25)(25 分)

PAT Basic 1015

PAT乙级 1015 德才论 (25分)

PAT乙级 1015 德才论 (25 分)

PAT——乙级1015/甲级1062:德才论

Basic Level 1015 - 德才论