Note
- 排序
- compare排序时 string相对于int要快(比如6位数的ID号)
(用int 最后一个测试点会超时)
Code:
#include<bits/stdc++.h>
using namespace std;
struct student{
string id;
string name;
int grade;
}stu[100005];
int c=-1;
bool compare(student a,student b){
if(c==1) return a.id<b.id;
else if(c==2){
if(a.name!=b.name) return a.name<b.name;
else return a.id<b.id;
}
else if(c==3){
if(a.grade!=b.grade) return a.grade<b.grade;
else return a.id<b.id;
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int n;
cin>>n>>c;
for(int i=0;i<n;i++){
cin>>stu[i].id>>stu[i].name>>stu[i].grade;
}
sort(stu,stu+n,compare);
for(int i=0;i<n;i++){
cout<<stu[i].id<<" "<<stu[i].name<<" "<<stu[i].grade<<endl;
}
return 0;
}