PAT甲级 1055 The World‘s Richest

该博客介绍了如何解决PAT编程竞赛中的一道题目,涉及年龄排序和在特定范围内查找指定数量的匹配项。代码实现包括两个版本,分别使用了vector和map数据结构,对年龄进行筛选和限制查询,确保结果不超过10个。

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

由于每个询问的个数不多于100个,所以给到每个年龄对应的个数应该小于10个左右 

#include<iostream>
#include<vector>
#include<string>
#include<bits/stdc++.h>

using namespace std;

typedef struct {
    string nam;
    int age;
    int mon;
}worthy;

bool compare(const worthy& a, const worthy& c){
    if(a.age==c.age and a.mon==c.mon){
        if(a.nam < c.nam) return true;
        else return false;
    }else if(a.mon==c.mon){
        if(a.age < c.age) return true;
        else return false;
    }else{
        if(a.mon > c.mon) return true;
        else return false;
    }
}

int main(void){
    int i, j, k, m, n, h, mm, nn, age, mar, cnt;
    int x, y;
    cin>>m>>n;
    vector<worthy> v[201], v0;
    worthy wy;
    for(i = 0; i < m; i++){
        cin>>wy.nam>>wy.age>>wy.mon;
        v[wy.age].push_back(wy);
    }
    for(i = 0; i < 201; i++){
        if(v[i].size() > 10){
            sort(v[i].begin(), v[i].end(), compare);
            v[i].erase(v[i].begin()+10, v[i].end());
        }
    }
    for(i = 0; i < n; i++){
        cin>>h>>mm>>nn;
        cout<<"Case #"<<i+1<<":"<<endl;
        mar = 999;
        cnt = 0;
        vector<worthy> v0;
        for(j = mm; j <=nn; j++){
            for(x = 0; x < v[j].size(); x++)
                v0.push_back(v[j][x]);
        }
        if(v0.size() == 0){
            cout<<"None"<<endl;
            continue;
        }
        sort(v0.begin(), v0.end(), compare);
        cnt = min(h, (int)(v0.size()));
        for(j = 0; j < cnt; j++){
            cout<<v0[j].nam<<" "<<v0[j].age<<" "<<v0[j].mon<<endl;
        }
    }
    return 0;
}

// #include<iostream>
// #include<vector>
// #include<string>
// #include<map>
// #include<bits/stdc++.h>

// using namespace std;

// typedef struct {
//     string nam;
//     int age;
//     int mon;
// }worthy;

// bool compare(const worthy& a, const worthy& c){
//     if(a.age==c.age and a.mon==c.mon){
//         if(a.nam < c.nam) return true;
//         else return false;
//     }else if(a.mon==c.mon){
//         if(a.age < c.age) return true;
//         else return false;
//     }else{
//         if(a.mon > c.mon) return true;
//         else return false;
//     }
// }

// int main(void){
//     int i, j, k, m, n, h, mm, nn, age, mar, cnt;
//     cin>>m>>n;
//     vector<worthy> v, v0;
//     worthy wy;
//     map<int, int> agerank;
//     for(i = 0; i < m; i++){
//         cin>>wy.nam>>wy.age>>wy.mon;
//         v.push_back(wy);
//     }
//     sort(v.begin(), v.end(), compare);
//     for(i = 0; i < m; i++){
//         age = v[i].age;
//         if(agerank[age]<=10){
//             v0.push_back(v[i]);
//             agerank[age] += 1;
//         }
//     }
//     v = v0;
//     for(i = 0; i < n; i++){
//         cin>>h>>mm>>nn;
//         cout<<"Case #"<<i+1<<":"<<endl;
//         mar = 999;
//         cnt = 0;
//         for(j = 0; j < m; j++){
//             age = v[j].age;
//             if(age >= mm && age <= nn){
//                 cnt++;
//                 if(cnt>h) break;
//                 cout<<v[j].nam<<" "<<v[j].age<<" "<<v[j].mon<<endl;
//                 mar = -999;
//             }
//         }
//         if(mar>0){
//             cout<<"None"<<endl;
//         }
//     }
//     return 0;
// }

 GitHub - ZouJiu1/PAT: 浙江大学PAT题目解答内容浙江大学PAT题目解答内容. Contribute to ZouJiu1/PAT development by creating an account on GitHub.https://github.com/ZouJiu1/PAT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九是否随机的称呼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值