題意:給定n個人以及k個查詢,輸出Amin到Amax之間的財富排名榜
tip:模擬+sort
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
struct ss {
string name;
int age;
int money;
};
bool cmp(struct ss a,struct ss b) {
if(a.money==b.money) {
if(a.age==b.age)
return a.name<b.name;
return a.age<b.age;
}
return a.money>b.money;
}
int main() {
int n,m,checked[203]= {0};
scanf("%d %d\n",&n,&m);
vector<struct ss>ans;
for(int i=0; i<n; ++i) {
char name[100];
int age,money;
scanf("%s %d %d\n",name,&age,&money);
ans.push_back({name,age,money});
}
sort(ans.begin(),ans.end(),cmp);
for(int i=0; i<m; ++i) {
int k,a,b,t;
scanf("%d %d %d",&k,&a,&b);
t=k;
printf("Case #%d:\n",i+1);
for(auto it=ans.begin(); it!=ans.end(); ++it) {
if(!k)
break;
if((*it).age>=a&&(*it).age<=b) {
printf("%s %d %d\n",(*it).name.c_str(),(*it).age,(*it).money);
k--;
}
}
if(t==k)
cout<<"None\n";
}
return 0;
}