• PAT.1055 The world's Richest(排序,died)


    1055 The World's Richest (25分)

     

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world's wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the net worths of N people, you must find the M richest people in a given range of their ages.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤) - the total number of people, and K (≤) - the number of queries. Then N lines follow, each contains the name (string of no more than 8 characters without space), age (integer in (0, 200]), and the net worth (integer in [−]) of a person. Finally there are K lines of queries, each contains three positive integers: M (≤) - the maximum number of outputs, and [AminAmax] which are the range of ages. All the numbers in a line are separated by a space.

    Output Specification:

    For each query, first print in a line Case #X: where X is the query number starting from 1. Then output the M richest people with their ages in the range [AminAmax]. Each person's information occupies a line, in the format

    Name Age Net_Worth
    

    The outputs must be in non-increasing order of the net worths. In case there are equal worths, it must be in non-decreasing order of the ages. If both worths and ages are the same, then the output must be in non-decreasing alphabetical order of the names. It is guaranteed that there is no two persons share all the same of the three pieces of information. In case no one is found, output None.

    Sample Input:

    12 4
    Zoe_Bill 35 2333
    Bob_Volk 24 5888
    Anny_Cin 95 999999
    Williams 30 -22
    Cindy 76 76000
    Alice 18 88888
    Joe_Mike 32 3222
    Michael 5 300000
    Rosemary 40 5888
    Dobby 24 5888
    Billy 24 5888
    Nobody 5 0
    4 15 45
    4 30 35
    4 5 95
    1 45 50
    

    Sample Output:

    Case #1:
    Alice 18 88888
    Billy 24 5888
    Bob_Volk 24 5888
    Dobby 24 5888
    Case #2:
    Joe_Mike 32 3222
    Zoe_Bill 35 2333
    Williams 30 -22
    Case #3:
    Anny_Cin 95 999999
    Michael 5 300000
    Alice 18 88888
    Cindy 76 76000
    Case #4:
    None

    这题真的被自己写代码的思路气死了 微笑脸.jpg
    反思一下:我发现自己写代码老是往最好实现的方向来,又费时间又费空间,真的垃圾。写代码过脑子行吗。

    这题,要求最多输出m个,然后人家n巨大,用屁股想都知道需要剪枝一下,然后你并没有脑子,和屁股,吐了。

     1 #include <cstdio>
     2 #include <string>
     3 #include <algorithm>
     4 #include <vector>
     5 using namespace std;
     6 
     7 const int maxn = 100000 + 5;
     8 
     9 struct Peo {
    10     string name;
    11     int wealth;
    12     int age;
    13 };
    14 vector <Peo> peo, vaild;
    15 
    16 bool cmp(Peo a, Peo b) {
    17     if(a.wealth == b.wealth) {
    18         if(a.age == b.age) {
    19             return a.name < b.name;
    20         }
    21         return a.age < b.age;
    22     }
    23     return a.wealth > b.wealth;
    24 }
    25 
    26 int wealth_num[205];
    27 
    28 int main() {
    29     int n, k, _case = 0;
    30     scanf("%d %d", &n, &k);
    31     string name;
    32     int age, wealth;
    33     name.resize(10);
    34     for(int i = 0; i < n; i ++) {
    35         scanf("%s %d %d", &name[0], &age, &wealth);
    36         peo.push_back(Peo{name, wealth, age});
    37     }
    38     
    39     int m, l, r;
    40     Peo temp;
    41     sort(peo.begin(), peo.end(), cmp);
    42     for(int i = 0; i < n; i ++) {
    43         temp = peo[i];
    44         if(wealth_num[temp.age] ++ < 100) {
    45             vaild.push_back(temp);
    46         }
    47     }
    48     while(k --) {
    49         scanf("%d %d %d", &m, &l, &r);
    50         int num = 0;
    51         printf("Case #%d:
    ", ++_case);
    52         for(int i = 0; i < vaild.size(); i ++) {
    53             if(num == m) break;
    54             if(vaild[i].age >= l && vaild[i].age <= r) {
    55                 num ++;
    56                 printf("%s %d %d
    ", vaild[i].name.c_str(), vaild[i].age, vaild[i].wealth);
    57             }
    58         }
    59         if(num == 0) printf("None
    ");
    60     }
    61     return 0;
    62 }
     
  • 相关阅读:
    国内医保控费公司简单比较
    程序员生存定律--管理向左,技术向右
    程序员生存定律--细论软件这个行当的根本特征
    程序员生存定律--细论影响人生成绩的四个要素(2)
    程序员生存定律--细论影响人生成绩的四个要素(1)
    程序员生存定律--定律的概要
    程序员生存定律--交换是职场里一切的根本
    程序员生存定律--目录
    程序员生存定律--那个是你的人生出口
    程序员生存定律--程序人生的出口
  • 原文地址:https://www.cnblogs.com/bianjunting/p/12993464.html
Copyright © 2020-2023  润新知