【ACM】PAT. A1055 The World`s Richest【排序】

本文介绍了一种解决PAT-A1055问题的算法实现,通过C++编程语言完成对富豪榜数据的整体排序及筛选,具体包括读取数据、排序算法的设计与实现、以及按照特定条件进行数据检索的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接
题目分析
解题思路

先整体排序,再按条件遍历筛选即可。


AC程序(C++)
/**************************
//@Author: 3stone
//@ACM: PAT-A1055 The World`s Richest
//@Time: 18/1/27
//@IDE: VS2017
***************************/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>

using namespace std;

typedef struct {
    string name;
    int age, worth;
}Stu;

bool cmp(Stu s1, Stu s2) {
    if (s1.worth != s2.worth) return s1.worth > s2.worth;
    else if (s1.age != s2.age) return s1.age < s2.age;
    else return s1.name < s2.name;
}

Stu stu[100010];

int main() {

    int n, m;
    char name[10];

    //输入
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) { //input
        scanf("%s %d %d", name, &stu[i].age, &stu[i].worth);
        stu[i].name = name;
    }

    //整体排序
    sort(stu, stu + n, cmp);

    //search
    for (int i = 1; i <= m; i++) { 
        int num, low, high, count = 0;
        scanf("%d%d%d", &num, &low, &high);

        printf("Case #%d:\n", i);
        for (int j = 0; j < n && count < num; j++) {
            if (low <= stu[j].age && stu[j].age <= high) {
                printf("%s %d %d\n", stu[j].name.c_str(), stu[j].age, stu[j].worth);
                count++;
            }
        }
        if (count == 0) printf("None\n");
    }

    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值