这个用C++的话就是要用stable_sort()
,然后注意下循环输入while(cin>>n>>type)
,一开始我没注意
#include<vector>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct Stu{
string name;
int grade;
};
int main(){
int n,type;
while(cin>>n>>type){
vector<Stu> list;
for(int i=0;i<n;++i){
string name;
int grade;
cin>>name>>grade;
list.push_back({name,grade});
}
if(!type) stable_sort(list.begin(),list.end(),[](Stu x,Stu y){
return x.grade>y.grade;
});
else stable_sort(list.begin(),list.end(),[](Stu x,Stu y){
return x.grade<y.grade;
});
for(auto stu:list){
cout<<stu.name<<' '<<stu.grade<<endl;
}
}
return 0;
}