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. Mongoose初使用总结

    连接mongoose mongoose连接数据库有两种方式 第一种: 'use strict'; const mongoose = require('mongoose'); mongoose.conn ...

  2. Three.js入门详解

    什么是WebGL   WebGL(Web 图形库)是一种 JavaScript API,用于在任何兼容的 Web 浏览器中呈现交互式 3D 和 2D 图形,而无需使用插件.WebGL 通过引入一个与 ...

  3. JDK源码那些事儿之并发ConcurrentHashMap下篇

    上一篇文章已经就ConcurrentHashMap进行了部分说明,介绍了其中涉及的常量和变量的含义,有些部分需要结合方法源码来理解,今天这篇文章就继续讲解并发ConcurrentHashMap 前言 ...

  4. webpack 配置react脚手架(五):mobx

    1.  配置项.使用mobx,因为语法时es6-next,所以先配置 .babelrc 文件 { "presets": [ ["es2015", { " ...

  5. RF 中一条用例执行失败,终止其他用例执行

    1. 需求: 执行某个测试套时,某条用例执行失败,则该用例下其他关键字不在执行(RF自带功能): 但实际情况下是 某条用例执行失败后,下面的用例再执行就没有意义了: 想满足某条用例执行失败,下面的用例 ...

  6. ASP.NET MVC 入门6、TempData

    TempData用来给控制各Action间传递值,或Action给View传递临时值时使用. TempData实际是将值临时存储于Session中. TempData中存储的值只能供一次访问使用, 即 ...

  7. Jquery “This”的指向

    JavaScript中的this不总是指向当前对象,函数或类中的this指向与调用这个函数的对象以及上下文环境是息息相关的.如在全局作用域调用一个含this的对象,此时当前对象的this指向的是win ...

  8. 如何的keil试试调试中,看逻辑分析仪Logic viwer

    在调试过程中,可以使用keil自带的逻辑分析仪查看变量的试试信息,减少串口输出,提高部分cpu的效率,可以添加以下信息: 1.gpio引脚 2.全局变量 全局静态变量.局部变量是不行的. 然后,添加变 ...

  9. 将 Django 应用程序部署到生产服务器

    原文出自: http://www.ibm.com/developerworks/cn/opensource/os-django/ 比较有启发性质的一篇文章,会避免很多弯路 Django 是一个基于 P ...

  10. 001_git: 版本控制软件

    一.基础配置 1.安装]# yum install -y git 2.配置用户信息配置用户联系方式:名字.email]# git config --global user.name "Mr. ...