PAT 甲级 1055 The World's Richest (25 分)(简单题,要用printf和scanf,否则超时,string 的输入输出要注意)
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 [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.用结构体数组记录各项信息,设置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 的输入输出要注意)的更多相关文章
- PAT甲级:1036 Boys vs Girls (25分)
PAT甲级:1036 Boys vs Girls (25分) 题干 This time you are asked to tell the difference between the lowest ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT 甲级 1145 Hashing - Average Search Time (25 分)(读不懂题,也没听说过平方探测法解决哈希冲突。。。感觉题目也有点问题)
1145 Hashing - Average Search Time (25 分) The task of this problem is simple: insert a sequence of ...
- 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 ...
- 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 ...
- PAT 甲级 1039 Course List for Student (25 分)(字符串哈希,优先队列,没想到是哈希)*
1039 Course List for Student (25 分) Zhejiang University has 40000 students and provides 2500 cours ...
- 【PAT甲级】1055 The World's Richest (25 分)
题意: 输入两个正整数N和K(N<=1e5,K<=1000),接着输入N行,每行包括一位老板的名字,年龄和财富.K次询问,每次输入三个正整数M,L,R(M<=100,L,R<= ...
- 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 ...
- PAT甲级1055 The World's Richest【排序】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805421066272768 题意: 给定n个人的名字,年龄和身价. ...
随机推荐
- 如何在linux系统下查看日志
在linux系统下, 首先在idea中使用clean---->install----->package将这个项目进行打包,打包的方式 , 根据你在项目中的pom文件,最上面,可以查看到 这 ...
- 前端学习笔记--CSS入门
1.css概述: 2.css语法: 3.css添加方法: 用单独的文件存储css样式的优点: 优先级: h3得到的样式是内嵌样式覆盖了外部样式. 4.css选择器 标签选择器: 类别选择器: ID选择 ...
- MyBatsi学习
深入浅出Mybatis系列(一)---Mybatis入门 深入浅出Mybatis系列(二)---配置简介(mybatis源码篇) 深入浅出Mybatis系列(三)---配置详解之properties与 ...
- sql sever 触发器的概念和使用
触发器简介: 触发器是一种特殊的存储过程,它的执行不是由程序调用,也不是手动执行,而是由事件来触发.触发器是当对某一个表进行操作.例如:update.insert.delete这些操作的时候,系统会自 ...
- Navicat连接Oracle报ORA-12737错误
替换oci.dll 文件分享百度网盘:链接:https://pan.baidu.com/s/1wayojGlKcgdMRZTvBqAUgw 密码:3d6j 把下载的文件放到Navicat文件夹里,然后 ...
- 002_Python3 基础语法
1.注释 实例1: #!/usr/bin/python3 # 第一个注释 print("Hello, Python!") # 第二个注释 ****************** ...
- 使用webuploader实现断点续传
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- 通过 Ajax 调取后台接口将返回的 json 数据绑定在页面上
第一步: 编写基础的 html 框架内容,并引入 jquery: <!doctype html> <html lang="en"> <head> ...
- [English] - 单词阶段1
百词斩这个app很好玩,尤其是在记忆单词的时候,效果显著. 有的PK赛场也是比较谁的单词翻译提交的快,这个我曾经连胜好几次.
- LibreOJ #6165. 一道水题
二次联通门 : LibreOJ #6165. 一道水题 /* LibreOJ #6165. 一道水题 欧拉线性筛 其实题意就是求区间[1, n]所有数的最小公倍数 那么答案就是所有质因子最大幂次的乘积 ...