1062. Talent and Virtue (25)解题报告

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 100000;

struct people {
    char id[15];
    int virtue, talent;
};
bool comp(people p1, people p2);

void copy(people &p1, people &p2) {
    strcpy(p1.id, p2.id);
    p1.virtue = p2.virtue;
    p1.talent = p2.talent;
    return;
}
void print(people p[], int cnt);

int main(void) {
    int sage_cnt, noble_cnt, fool_cnt, small_cnt, i, n, l, h;
    people *noble = new people[N], *sage = new people[N], *fool = new people[N], *small = new people[N], tmp;
    scanf("%d %d %d", &n, &l, &h);
    sage_cnt = noble_cnt = fool_cnt = small_cnt = 0;
    for (i = 0; i < n; i++) {
        scanf("%s %d %d", tmp.id, &tmp.virtue, &tmp.talent);
        if (tmp.virtue >= h && tmp.talent >= h) {
            copy(sage[sage_cnt], tmp);
            sage_cnt++;
        }
        else if (tmp.virtue >= h && tmp.talent >= l) {
            copy(noble[noble_cnt], tmp);
            noble_cnt++;
        }
        else if (tmp.virtue >= l && tmp.talent >= l && tmp.virtue >= tmp.talent) {
            copy(fool[fool_cnt], tmp);
            fool_cnt++;
        }
        else if (tmp.virtue >= l && tmp.talent >= l) {
            copy(small[small_cnt], tmp);
            small_cnt++;
        }
    }
    sort(sage, sage + sage_cnt, comp);
    sort(noble, noble + noble_cnt, comp);
    sort(fool, fool + fool_cnt, comp);
    sort(small, small + small_cnt, comp);
    printf("%d\n", sage_cnt + noble_cnt + fool_cnt + small_cnt);
    print(sage, sage_cnt);
    print(noble, noble_cnt);
    print(fool, fool_cnt);
    print(small, small_cnt);
    delete[] sage;
    delete[] noble;
    delete[] fool;
    delete[] small;
    return 0;
}
bool comp(people p1, people p2) {
    if (p1.virtue + p1.talent > p2.virtue + p2.talent) {
        return true;
    }
    else if (p1.virtue + p1.talent < p2.virtue + p2.talent) {
        return false;
    }
    else {
        if (p1.virtue > p2.virtue) {
            return true;
        }
        else if (p1.virtue < p2.virtue) {
            return false;
        }
        else {
            int result = strcmp(p1.id, p2.id);
            if (result < 0) {
                return true;
            }
            else {
                return false;
            }
        }
    }
}

void print(people p[], int cnt) {
    int i;
    for (i = 0; i < cnt; i++) {
        printf("%s %d %d\n", p[i].id, p[i].virtue, p[i].talent);
    }
    return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值