题意:给出人数 第几列排序
排序有三种情况
1:给ID排序
2:给姓名排序 如果一样则看ID
3:给分数排序,如果分数一样 看ID
题解:本题难度不大,主要是要对字符串的排序要熟悉,不能只会数字排序(确实也没考虑到字符串也能直接排序),对sort的功能了解的也更多了。
详细代码样例:
#include <bits/stdc++.h>
using namespace std;
struct student{
string ID;
int Grade;
string Name;
};
bool id(student &x,student &y){
return x.ID<y.ID;
}
bool iname(student &x,student &y){//字符串的排序,可以加限制条件 ,返回的是bool型就可以了
if(x.Name==y.Name) return x.ID<y.ID;
else return x.Name<y.Name;
}
bool igrade(student &x,student &y){
if(x.Grade!=y.Grade) return x.Grade<y.Grade;
return x.ID <y.ID;
}
int main(){
int M,N;
cin>>M>>N;
student s[100001];
for(int i=0;i<M;i++){
cin>>s[i].ID>>s[i].Name>>s[i].Grade;
}
if(N==1){
sort(s,s+M,id);
}
if(N==2){
sort(s,s+M,iname);
}
if(N==3){
sort(s,s+M,igrade);
}
for(int i=0;i<M;i++){
cout<<s[i].ID<<' '<<s[i].Name<<' '<<s[i].Grade<<endl;
}
}