测试点一超时是因为你用了cin,cout,搞得程序很慢,所以以后写pat的题目别用cin,cout,用的时候很爽,改的时候就知道错了,还有不要用scanf输入string,string是c++的,要用cin,所以这题如果名字在结构体用了string定义就要改成char name[10]了,bool cmp的时候就不能用return a.name
测试点二超时的话,我估计你的方法就是先用一个数组全部存起来,然后在做一个临时数组vector,把符合条件的数据都存在vector里面,然后用完再v.cear,但是,你看看你的sort再在哪里,是不是sort了很多次,所以测试点2才会超时,别问问就是我原来就是这么干的,改成只sort一次,再整一个计数器,要输出多少个就输出多少个就好了;
代码:
#include
#include
#include
#include
#include
using namespace std;
struct node{
int age;
char name[10];
int worth;
};
bool cmp(node a,node b){
if(a.worth==b.worth&&a.age==b.age){
return strcmp(a.name,b.name)<0;
}else if(a.worth==b.worth){
return a.ageb.worth;
}
}
int main(){
int n,m,column,age1,age2,flag=0,cnt=0;
cin>>n>>m;
vector v(n);
for(int i=0;i=age1&&v[j].age<=age2&&cnt!=column){
cout<