先统一排序,再按照条件输出
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
struct Person{
char name[10];
int age;
int worth;
Person(){}
Person(char n[], int a, int w) : age(a), worth(w){
strcpy(name, n);
}
bool operator < (const Person& rhs) const{
return worth > rhs.worth
|| (worth == rhs.worth && age < rhs.age)
|| (worth == rhs.worth && age == rhs.age && strcmp(name, rhs.name) < 0);
}
};
int main(){
int n, k;
scanf("%d%d", &n, &k);
vector<Person> people(n);
for(int i = 0; i < n; ++i){
scanf("%s%d%d", people[i].name, &people[i].age, &people[i].worth);
}
sort(begin(people), end(people));
for(int i = 0; i < k; ++i){
int m, amin, amax;
scanf("%d%d%d", &m, &amin, &amax);
printf("Case #%d:\n", i+1);
int sum = 0;
for(int j = 0; j < n; ++j){
if(sum >= m) break;
if(people[j].age >= amin && people[j].age <= amax){
++sum;
printf("%s %d %d\n", people[j].name, people[j].age, people[j].worth);
}
}
if(sum == 0) printf("None\n");
}
return 0;
}