解法一:排序
按照题目要求排序,然后对于每个查询,遍历数组输出年龄满足要求的人。
#include <bits/stdc++.h>
using namespace std;
class People {
public:
string name;
int age;
int worth;
};
bool cmp(People &o1, People &o2) {
if (o1.worth == o2.worth) {
if (o1.age == o2.age) {
return o1.name < o2.name;
} else {
return o1.age < o2.age;
}
} else {
return o1.worth > o2.worth;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, K;
cin >> N >> K;
People peoples[N];
for (int i = 0; i < N; ++i) {
cin >> peoples[i].name >> peoples[i].age >> peoples[i].worth;
}
sort(peoples, peoples + N, cmp);
int M, AMin, AMax;
for (int index = 1; index <= K; ++index) {
cin >> M >> AMin >> AMax;
int cnt = 0;
cout << "Case #" << index << ":\n";
for (auto &people:peoples) {
if (cnt >= M) {
break;
}
if (people.age >= AMin && people.age <= AMax) {
cout << people.name << " " << people.age << " " << people.worth << "\n";
++cnt;
}
}
if (cnt == 0) {
cout << "None\n";
}
}
}