加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

PAT A1055 The World's Richest

发布时间:2020-12-14 05:08:18 所属栏目:大数据 来源:网络整理
导读:PAT A1055 The World‘s Richest 题目描述: 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 pe

PAT A1055 The World‘s Richest

题目描述:

  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 (≤10?5??) - the total number of people,and K (≤10?3??) - 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 [?10?6??,10?6??]) of a person. Finally there are K lines of queries,each contains three positive integers: M (≤100) - the maximum number of outputs,and [Amin,Amax] 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 [Amin,Amax]. 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

参考代码:

 1 /**************************************************
 2 PAT A1055 The World‘s Richest
 3 **************************************************/
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <vector>
 7 
 8 using namespace std;
 9 
10 struct peopleInfo {
11     string name;
12     int age = 0;
13     int wealth = 0;
14 };
15 
16 //自定义比较函数
17 bool cmp(peopleInfo a,peopleInfo b) {
18     if (a.wealth == b.wealth) {
19         if (a.age == b.age) {
20             return a.name < b.name;
21         } return a.age < b.age;
22     } else return a.wealth > b.wealth;
23 }
24 
25 int main() {
26     //读入信息
27     vector<peopleInfo> PeopleList;
28     int peopleCnt = 0,queryCnt = 0;
29 
30     cin >> peopleCnt >> queryCnt;
31 
32     peopleInfo temp;
33     for (int i = 0; i < peopleCnt; ++i) {
34         cin >> temp.name >> temp.age >> temp.wealth;
35         PeopleList.push_back(temp);
36     }
37 
38     //对录入信息进行排序
39     sort(PeopleList.begin(),PeopleList.end(),cmp);
40 
41     //按要求输出结果
42     for (int i = 0; i < queryCnt; ++i) {
43         if (i != 0) cout << endl;                  //避免多余换行
44         cout << "Case #" << i + 1 <<  << endl;  //注意这个:号
45 
46         int maxPrint = 0,minAge = 0,maxAge = 0,printCnt = 0;
47 
48         cin >> maxPrint >> minAge >> maxAge;
49 
50         for (int j = 0; j < peopleCnt && printCnt < maxPrint; ++j) {           //注意添加printCnt < maxPrint的限制,否则在输入信息很多的情况下会超时
51             if (PeopleList[j].age >= minAge && PeopleList[j].age <= maxAge) {
52                 if (printCnt != 0) cout << endl;   //避免多余换行,输出个数未知
53 
54                 cout << PeopleList[j].name <<   << PeopleList[j].age <<   << PeopleList[j].wealth;
55                 printCnt++;
56             }
57         }
58 
59         if (printCnt == 0) cout << "None";         //没有符合条件的输出时输出None
60     }
61 
62     return 0;
63 }

注意事项:

  1:这道题花费的时间挺久的,每次都是答案错误和多种错误,而且最近感冒脑子不是很清楚,到最后才发现自己少了44行中的‘:‘符号,希望各位也能在写代码的时候看清要求避免不必要的时间浪费。

  2:身体是革命的本钱,多注意自己的身体。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读