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

题意:

1.用结构体数组记录各项信息,设置cmp排序函数。 
2.按要求读入,并排序。 
3.根据输出对人数和年龄段的限制,遍历排序后的数组,符合要求就输出,达到制定人数就退出。 
4.每一个查询,设置cnt记录已经输出的人数,如果为0,要额外输出None

题解:

string 的输入输出要注意

string scanf()

s.resize(10); //需要预先分配空间
scanf("%s", &s[0]);

string printf()

string s;
s="fdasf";
printf("%s\n",s.c_str());

或者用char

struct Node{
int age,worth;
char name[];
}node[maxn]; bool cmp(Node a,Node b){
if(a.worth !=b.worth )
return a.worth >b.worth ;
else if(a.age !=b.age )
return a.age <b.age ;
else
return strcmp(a.name ,b.name )<;
} scanf("%s %d %d",node[i].name ,&node[i].age ,&node[i].worth );

AC代码:

#include<iostream>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
string name;
int age;
int money;
}a[];
bool cmp(node x,node y){
if(x.money==y.money){
if(x.age==y.age){
return x.name<y.name;
}else{
return x.age<y.age;
}
}else{
return x.money>y.money;
}
}
int n,k;
int m,ua,va;
int main(){
scanf("%d %d",&n,&k);
for(int i=;i<=n;i++){
a[i].name.resize(); //需要预先分配空间
scanf("%s %d %d", &a[i].name[],&a[i].age,&a[i].money);
}
sort(a+,a++n,cmp);
for(int i=;i<=k;i++){
printf("Case #%d:\n",i);
scanf("%d %d %d",&m,&ua,&va);
int p=;
for(int j=;j<=n;j++){
if(a[j].age>=ua && a[j].age<=va){
p++;
printf("%s %d %d\n",a[j].name.c_str(),a[j].age,a[j].money);
}
if(p==m){
break;
}
}
if(p==){
printf("None");
}
}
return ;
}

PAT 甲级 1055 The World's Richest (25 分)(简单题,要用printf和scanf,否则超时,string 的输入输出要注意)的更多相关文章

  1. PAT甲级:1036 Boys vs Girls (25分)

    PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...

  2. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  3. PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)

    1145 Hashing - Average Search Time (25 分)   The task of this problem is simple: insert a sequence of ...

  4. PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

    1066 Root of AVL Tree (25 分)   An AVL tree is a self-balancing binary search tree. In an AVL tree, t ...

  5. PAT 甲级 1047 Student List for Course (25 分)(cout超时,string scanf printf注意点,字符串哈希反哈希)

    1047 Student List for Course (25 分)   Zhejiang University has 40,000 students and provides 2,500 cou ...

  6. PAT 甲级 1039 Course List for Student (25 分)(字符串哈希,优先队列,没想到是哈希)*

    1039 Course List for Student (25 分)   Zhejiang University has 40000 students and provides 2500 cours ...

  7. 【PAT甲级】1055 The World's Richest (25 分)

    题意: 输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富.K次询问,每次输入三个正整数M,L,R(M<=100,L,R<= ...

  8. PAT (Advanced Level) Practice 1055 The World's Richest (25 分) (结构体排序)

    Forbes magazine publishes every year its list of billionaires based on the annual ranking of the wor ...

  9. PAT甲级1055 The World's Richest【排序】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768 题意: 给定n个人的名字,年龄和身价. ...

随机推荐

  1. UML之九种图

    UML说是九种图吧!其实是众说纷纭,不管有几种图,我们只要能够很好的运用这几张图就好,主要有用例图.类图.对象图.状态图.活动图.序列图.协作图.构件图和部署图,至于包图是否属于这九种图,我也理不清楚 ...

  2. Java并发包--ConcurrentHashMap原理解析

    ConcurrentHashMap实现原理及源码分析   ConcurrentHashMap是Java并发包中提供的一个线程安全且高效的HashMap实现(若对HashMap的实现原理还不甚了解,可参 ...

  3. comm shell command

    1.awk command 1.1 Purpose 1: want to distinct and then count and sort by num 1.1.1 Command: cat resu ...

  4. 多任务创建-线程(IO密集型适用)

    单核CPU:时间片轮转 并行:CPU的个数大于等于任务数 真的多任务执行 并发:CPU的个数小于任务数 假的多任务 知识点: 多线程共享全局变量 创建线程的两种方法: 1.创建子线程方法 调用函数 T ...

  5. [Dart] Understand Classes and Inheritance in Dart

    We will look at how we can create classes and explore some various features. Dart adopts a single-in ...

  6. javascript动态合并表格相同的单元格

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  7. IDEA去掉屏幕中间的白色竖线

    现状: 打开File->Settings->Editor->General->Appearance,去掉勾选: 修改后:

  8. Greenplum table 之 appendonly的列存储表

    一.appendonly的列存储表 1.创建列存储表 create table test_column_ao( id bigint, name varchar(128), value varchar( ...

  9. P1213 时钟

    题目描述 考虑将如此安排在一个 3 x 3 行列中的九个时钟: 目标要找一个最小的移动顺序将所有的指针指向12点.下面原表格列出了9种不同的旋转指针的方法,每一种方法都叫一次移动.选择1到9号移动方法 ...

  10. Java常用类、集合框架类1

    A   时间日期格式转换(SDUT 2246)(SimpleDateFormat用法) 转换的df2里面时间是US,上面的df1可以是CHINA或者US. package test; import j ...