Note
-
排序
-
“the output must be in non-decreasing alphabetical order of the names”
-
按字母顺序非降序排列(此时,不能用string直接排!!!要存放在char[]中,用strcmp)
-
写cmp函数的时候 return strcmp(a.name, b.name) <= 0;
return语句返回的是true或者false的值,所以要写<= 0这样的形式。比较ACSII码的大小,strcmp(‘a’, ‘z’)返回负值,因为a<z a – z < 0
-
按照分数(int)的不降序排序,a.score <= b.score
-
此外,学会看题目条件!!!N (人数) 有105个,要求输出的人最多只有100个,差距非常大。
由此,可以先排序再从1~100岁里每个年龄段挑选出100个人 (按顺序加到vector数组中) ,这样再挑选输出,时间消耗会少很多。
Code:
#include<bits/stdc++.h>
using namespace std;
struct person{
char name[10];
int age,worth;
}p[100005];
bool compare(person a,person b){
if(a.worth!=b.worth) return a.worth>b.worth;
else if(a.age!=b.age) return a.age<b.age;
else return (strcmp(a.name, b.name)<0);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int n,m,t;
int max,amin,amax;
int count[202]={0};
vector<person> v; //借鉴柳神Code优化
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>p[i].name>>p[i].age>>p[i].worth;
sort(p,p+n,compare);
for(int i=0;i<n;i++){
if(count[p[i].age]<100){
v.push_back(p[i]);
count[p[i].age]++;
}
}
for(int j=1;j<=m;j++){
cin>>max>>amin>>amax;
t=max;
cout<<"Case #"<<j<<":"<<endl;
for(int k=0;k<v.size();k++){
if(v[k].age>=amin&&v[k].age<=amax){
t--;
cout<<v[k].name<<" "<<v[k].age<<" "<<v[k].worth<<endl;
}
if(t==0) break;
}
if(t==max) cout<<"None\n";
}
return 0;
}