【1028 25 排序】 List Sorting
传送门
题意
给定 \(n\) 条记录,每条记录为 \((id,name,grade)\) ,给定 \(c\) ,
- \(c=1\),按照 \(id\) 升序排序
- \(c=2\),按照 \(name\) 升序排序
- \(c=3\),按照 \(grade\) 升序排序
如果 \(name、grade\) 相同按照 \(id\) 升序
数据范围
\(n\leq 10^{5}\)
题解
- 用到
function,即函数的基本类型,可以是普通函数,可以是lambda函数 - 声明
function<返回值类型(函数参数)>类型的vector数组调用不用的lambda函数即可
Code
#include<bits/stdc++.h>
using namespace std;
struct node{
int id,gra;
string name;
};
bool cmp1(node a,node b){ return a.id<b.id; }
bool cmp2(node a,node b){
if(a.name==b.name)
return a.id<b.id;
return a.name<b.name;
}
bool cmp3(node a,node b){
if(a.gra==b.gra)
return a.id<b.id;
return a.gra<b.gra;
}
int main(){
int n,c; cin>>n>>c;
vector<node>a(n);
for(int i=0;i<n;i++)
cin>>a[i].id>>a[i].name>>a[i].gra;
if(c==1) sort(a.begin(),a.end(),cmp1);
else if(c==2) sort(a.begin(),a.end(),cmp2);
else sort(a.begin(),a.end(),cmp3);
for(auto it:a){
printf("%06d ",it.id);
cout<<it.name<<' '<<it.gra<<endl;
}
}

浙公网安备 33010602011771号