id用int,避免了id的strcmp,不然用string就超时。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
int n,c;
struct student{
int id;
char name[10];
int grade;
};
vector<student> stu;
bool cmp(student a,student b){
if(c==1){
return a.id<b.id;
}else if(c==2){
if(strcmp(a.name,b.name)!=0){
return strcmp(a.name,b.name)<0;
}else{
return a.id<b.id;
}
return 0;
}else if(c==3){
if(a.grade!=b.grade){
return a.grade<b.grade;
}else{
return a.id<b.id;
}
}
return 0;
}
int main(){
cin>>n>>c;
for(int i=0;i<n;i++){
student s;
scanf("%d%s%d",&s.id,s.name,&s.grade);
stu.push_back(s);
}
sort(stu.begin(),stu.end(),cmp);
std::vector<student>::iterator v=stu.begin();
while(v!=stu.end()){
printf("%06d %s %d\n",v->id,v->name,v->grade);
v++;
}
return 0;
}