zoukankan      html  css  js  c++  java
  • PAT 1062 Talent and Virtue

    #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
    ", 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
    ", count);
            
        sort_print(sage);
        sort_print(noble);
        sort_print(fool);
        sort_print(small);
    
        return 0;
    }

    有时排序,差个ranking

  • 相关阅读:
    VirtualBox 安装增强工具Guest Additions 【转载】
    全面认识计算机启动过程【转载】
    Linux下使用mke2fsk格式化虚拟磁盘分区的方法
    什么是posix
    bochs默认没有pci的
    剑指offer中二进制中1的个数
    原码、补码、反码的概念
    初识hadoop
    chord原理的解读
    深度学习概述:从感知机到深度网络
  • 原文地址:https://www.cnblogs.com/lailailai/p/4084046.html
Copyright © 2011-2022 走看看