原题链接:
代码:
#include <iostream>
#include <map>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
struct person {
string name;
int age;
int worth;
};
int N, K;
int M;
vector<person> v;
void query(int l, int r) {
int has = 0;
for (int i = 0; i < v.size(); i++) {
if (v[i].age <= r && v[i].age >= l) {
cout << v[i].name << " " << v[i].age <<" "<< v[i].worth << endl;
has++;
}
if (has == M) {
return;
}
}
if (has == 0) {
cout << "None" << endl;
}
return;
}
bool cmp(person& a, person& b) {
if (a.worth == b.worth) {
if (a.age == b.age) {
return a.name < b.name;
}
return a.age < b.age;
}
return a.worth > b.worth;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> N >> K;
//输入每个人的具体情况
for (int i = 0; i < N; i++) {
string tName;
int tAge, tWorth;
cin >> tName >> tAge >> tWorth;
struct person tPerson;
tPerson.name = tName;
tPerson.age = tAge;
tPerson.worth = tWorth;
v.push_back(tPerson);
}
sort(v.begin(), v.end(), cmp);
//输入查询条件
for (int i = 0; i < K; i++) {
cout << "Case #" << i + 1 << ":" << endl;
cin >> M;
int Amin, Amax;
cin >> Amin >> Amax;
query(Amin, Amax);
}
return 0;
}