【PAT甲级】1028 List Sorting (25 分)

题意:

输入一个正整数N(<=100000)和C(C属于{1,2,3}),接下来输入N行,每行包括学生的六位学号(习惯用string输入,因为可能有前导零),名字和成绩(正整数)。输出排序后的信息,排序根据C决定。

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
typedef struct student{
string id,name;
int grade;
};
student s[100007];
bool cmp(student a,student b){
return a.id<b.id;
}
bool cmp2(student a,student b){
if(a.name!=b.name)
return a.name<b.name;
return a.id<b.id;
}
bool cmp3(student a,student b){
if(a.grade!=b.grade)
return a.grade<b.grade;
return a.id<b.id;
}
int main(){
int n,c;
cin>>n>>c;
for(int i=1;i<=n;++i)
cin>>s[i].id>>s[i].name>>s[i].grade;
if(c==1)
sort(s+1,s+1+n,cmp);
else if(c==2)
sort(s+1,s+1+n,cmp2);
else if(c==3)
sort(s+1,s+1+n,cmp3);
for(int i=1;i<=n;++i){
cout<<s[i].id<<" "<<s[i].name<<" "<<s[i].grade;
cout<<((i==n)?"":"\n");
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/11481964.html