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. de4dot FAQ

    How to deobfuscate but make sure metadata tokens stay the same? --preserve-tokens will preserve all ...

  2. can't assign to struct fileds in map

    原文: https://haobook.readthedocs.io/zh_CN/latest/periodical/201611/zhangan.html --------------------- ...

  3. 「数据结构与算法之链表(Python)」(四)

    什么是链表 顺序表的储存分为一体式结构和分离式结构,但总的来说存储数据的内存是一块连续的单元,每次申请前都要预估所需要的内存空间大小.这样就不能随意的增加我们需要的数据了.链接就是为了解决这个问题.它 ...

  4. 包,logging日志模块,copy深浅拷贝

    一 包 package 包就是一个包含了 __init__.py文件的文件夹 包是模块的一种表现形式,包即模块 首次导入包: 先创建一个执行文件的名称空间 1.创建包下面的__init__.py文件的 ...

  5. C# 通过 参数返回 C++ 指针

    参数返回 C++ 指针 C++ 代码 Extern_C BASECORELIBRARY_API char * GetFileByteArray(wchar_t * BinfilePath, wchar ...

  6. Linux 查看系统配置参数

    原文链接:http://www.cnblogs.com/aric2016/p/10971690.html 查看 cpu信息: cat /proc/cpuinfo 查看内存信息: grep MemTot ...

  7. [USACO]骑马修栅栏 Riding the Fences

    题目链接 题目简述:欧拉回路,字典序最小.没什么好说的. 解题思路:插入边的时候,使用multiset来保证遍历出出答案的字典序最小. 算法模板:for(枚举边) 删边(无向图删两次) 遍历到那个点 ...

  8. leetcode解题报告(22):Two Sum II - Input array is sorted

    描述 Given an array of integers that is already sorted in ascending order, find two numbers such that ...

  9. Cogs 1714. [POJ1741][男人八题]树上的点对(点分治)

    [POJ1741][男人八题]树上的点对 ★★★ 输入文件:poj1741_tree.in 输出文件:poj1741_tree.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] ...

  10. python一些实用的小工具

    1  搭一个简易的本地局域网  python -m http.server 2 获取当前目录下的所有文件名 3 进度条效果 import sys,time for i in range(50): sy ...