IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> PTA甲级 1055 The World‘s Richest (C++) -> 正文阅读

[C++知识库]PTA甲级 1055 The World‘s Richest (C++)

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 N N people, you must find the M M 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 ( ≤ 1 0 5 ) N (≤10^5) N(105) - the total number of people, and K ( ≤ 1 0 3 ) K (≤10^3) K(103) - the number of queries. Then N N 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 [ ? 1 0 6 , 1 0 6 ] [?10^6,10^6] [?106,106]) of a person. Finally there are K K K lines of queries, each contains three positive integers: M ( ≤ 100 ) M (≤100) M(100) - 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 M 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

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-08-18 22:32:27
// All rights reserved.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Rich{
    string name;
    int age, worth;

    Rich(string n = "", int a = -1, int w = -1){
        name = n;
        age = a;
        worth = w;
    }

    ~Rich(){}
};

bool cmp(Rich &a, Rich &b){
    if(a.worth != b.worth) return a.worth > b.worth;
    if(a.age != b.age) return a.age < b.age;
    return a.name < b.name;
}

int main(){
    int n, m;
    scanf("%d %d", &n, &m);

    vector<Rich> riches;
    string name;
    int age, worth;

    for(int i = 0; i < n; ++i){
        cin >> name >> age >> worth;
        riches.emplace_back(name, age, worth);
    }

    sort(riches.begin(), riches.end(), cmp);

    for(int i = 0; i < m; ++i){
        int num;
        int ageMin, ageMax;
        scanf("%d %d %d", &num, &ageMin, &ageMax);

        int j = 0, pos = 0;

        printf("Case #%d:\n", i + 1);

        while(j < num && pos < n){
            if(riches[pos].age >= ageMin && riches[pos].age <= ageMax){
                printf("%s %d %d\n", riches[pos].name.c_str(), riches[pos].age, riches[pos].worth);
                j++;
            }
            pos++;
        }

        if(j == 0) printf("None\n");
    }

    return 0;
}

在这里插入图片描述

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-19 11:52:27  更:2021-08-19 11:53:12 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 13:48:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码