码迷,mamicode.com
首页 > 其他好文 > 详细

PAT 1062 Talent and Virtue

时间:2014-11-08 22:05:42      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   sp   for   div   on   

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

class Man {
public:
    char id[10];
    int talent;
    int virtue;
};


bool mycmp(const Man& a, const Man& b) {
    int ta = a.virtue + a.talent;
    int tb = b.virtue + b.talent;
    if (ta > tb) {
        return true;
    } else if (ta < tb) {
        return false;
    }
    // virtue + talent are equal, so compare virtue
    if (a.virtue > b.virtue) {
        return true;
    } else if (a.virtue < b.virtue) {
        return false;
    }
    // virtue is equal, so compare id
    return strcmp(a.id, b.id) < 0;
}

void sort_print(vector<Man> &v) {
    sort(v.begin(), v.end(), mycmp);
    int len = v.size();
    for (int i=0; i<len; i++) {
        printf("%s %d %d\n", v[i].id, v[i].virtue, v[i].talent);
    }
}

int main() {
    int N, L, H;
    scanf("%d%d%d", &N, &L, &H);
    vector<Man> sage;
    vector<Man> noble;
    vector<Man> fool;
    vector<Man> small;

    Man tmp;
    int count = 0;
    for (int i=0; i<N; i++) {
        scanf("%s%d%d", tmp.id, &(tmp.virtue), &(tmp.talent));
        if (tmp.virtue < L || tmp.talent < L) {
            // discard
            continue;
        }
        count++;
        if (tmp.virtue >= H && tmp.talent >= H) {
            sage.push_back(tmp);
        } else if (tmp.virtue >= H) {
            noble.push_back(tmp);
        } else if (tmp.virtue >= tmp.talent) {
            fool.push_back(tmp);
        } else {
            small.push_back(tmp);
        }
    }
    
    printf("%d\n", count);
        
    sort_print(sage);
    sort_print(noble);
    sort_print(fool);
    sort_print(small);

    return 0;
}

有时排序,差个ranking

PAT 1062 Talent and Virtue

标签:style   blog   io   color   ar   sp   for   div   on   

原文地址:http://www.cnblogs.com/lailailai/p/4084046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!