题目翻译:
给定一系列富人的财富记录(包括财富值、年龄、姓名),你需要根据输入的年龄范围输出该年龄段内最有钱的N位富豪。
题解思路:
按照题目要求自定义一个排序函数,模拟即可
代码:
#include<bits/stdc++.h>
using namespace std;
int N, K;//总人数,要查询的总次数
struct member {
string name;
int age;
int worth;
};
bool comp(member a, member b){
if (a.worth == b.worth && a.age == b.age) return a.name < b.name;
else if (a.worth == b.worth) return a.age < b.age;
else return a.worth > b.worth;
}
int main()
{
cin >> N >> K;
vector<member> members;
for (int i = 0;i < N;i++){
member temp;
cin >> temp.name >> temp.age >> temp.worth;
members.push_back(temp);
}
sort(members.begin(), members.end(), comp);
for (int i = 0;i < K;i++)
{
int output, age_low, age_high, num = 0;
vector<member> temp;
cin >> output >> age_low >> age_high;
cout << "Case #" << i + 1 << ":" << endl;
for (int j = 0;j < members.size() && num < output;j++){
if (members[j].age >= age_low && members[j].age <= age_high) {
cout << members[j].name << " " << members[j].age << " " << members[j].worth << endl;
num++;
}
}
if (num==0) cout << "None" << endl;
}
}
坑点:
无