博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1055 The World's Richest
阅读量:4540 次
发布时间:2019-06-08

本文共 4117 字,大约阅读时间需要 13 分钟。

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 4Zoe_Bill 35 2333Bob_Volk 24 5888Anny_Cin 95 999999Williams 30 -22Cindy 76 76000Alice 18 88888Joe_Mike 32 3222Michael 5 300000Rosemary 40 5888Dobby 24 5888Billy 24 5888Nobody 5 04 15 454 30 354 5 951 45 50

Sample Output:

Case #1:Alice 18 88888Billy 24 5888Bob_Volk 24 5888Dobby 24 5888Case #2:Joe_Mike 32 3222Zoe_Bill 35 2333Williams 30 -22Case #3:Anny_Cin 95 999999Michael 5 300000Alice 18 88888Cindy 76 76000Case #4:None 第一个代码是用vector存储,但遍历超时,第二个时使用数组存储,测试通过. 搞不明白同样的算法复杂度,为什么vector遍历时间就比数组差那么多?
1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 struct Node 7 { 8 string name; 9 int age, val;10 }node;11 int N, K, M, num, Amin, Amax;12 bool cmp(Node a, Node b)13 {14 if (a.val == b.val && a.age == b.age)15 return a.name < b.name;16 else if (a.val == b.val)17 return a.age < b.age;18 else19 return a.val > b.val;20 }21 int main()22 {23 cin >> N >> K;24 vector
v;25 vector
>res(K);26 for (int i = 0; i < N; ++i)27 {28 cin >> node.name >> node.age >> node.val;29 v.push_back(node);30 }31 sort(v.begin(), v.end(), cmp);32 for (int i = 0; i < K; ++i)33 {34 cin >> num >> Amin >> Amax;35 int flag = 0;36 cout << "Case #" << i + 1 << ":" << endl;37 for (auto a : v)38 {39 if (a.age >= Amin && a.age <= Amax)40 {41 cout << a.name << " " << a.age << " " << a.val << endl;42 flag++;43 if (flag == num)44 break;45 }46 }47 if (flag == 0)48 cout << "None" << endl;49 }50 return 0;51 }

 

1 #include
2 using namespace std; 3 struct Person{
//存储相应信息的结构体 4 string name; 5 int age,worth; 6 }; 7 Person person[(int)(1e5+5)]; 8 int main(){ 9 int N,K;10 scanf("%d%d",&N,&K);11 for(int i=0;i
>person[i].name>>person[i].age>>person[i].worth;13 sort(person,person+N,[](const Person&p1,const Person&p2){
//比较函数14 if(p1.worth!=p2.worth)15 return p1.worth>p2.worth;16 else if(p1.age!=p2.age)17 return p1.age
0;++j)//遍历数组27 if(person[j].age>=Amin&&person[j].age<=Amax){
//找到符合要求的人28 printf("%s %d %d\n",person[j].name.c_str(),person[j].age,person[j].worth);//输出29 --M;//将M递减30 f=true;//表示已输出过31 }32 if(!f)//在该年龄段没有任何输出33 printf("None\n");//输出None34 }35 return 0;36 }

 

 

转载于:https://www.cnblogs.com/zzw1024/p/11285996.html

你可能感兴趣的文章
python中的List,Tuple,Set,Dictionary
查看>>
JavaWeb 学习007-4个页面,5条sql语句(添加、查看、修改、删除)2016-12-2
查看>>
用JavaScript 来将数字转换成字符。
查看>>
扩展欧几里得算法
查看>>
java中的包装类
查看>>
采用多种算法,模拟摇奖:从1-36中随机抽出8个不重复的数字
查看>>
sp2.1 Practical aspects of Deep Learning
查看>>
java中的缓存
查看>>
2、文件夹
查看>>
DLL_Vs_CLL
查看>>
MakeFile
查看>>
AVAST 4.8
查看>>
6.1.1 web前端介绍
查看>>
jquery实现当前页面编辑
查看>>
初识rt-thread
查看>>
微服务架构下介质管理规范
查看>>
关于AutoCAD 2014的securityload…
查看>>
BM和KMP字符串匹配算法学习
查看>>
常用基本命令四(用户管理命令) - 黑猴子
查看>>
项目管理知识1
查看>>