成绩排序2
#include <bits/stdc++.h>
using namespace std;
struct MyType{
int num,score;
};
bool compare(MyType lhs,MyType rhs){
if(lhs.score<rhs.score){
return true;
}else if(lhs.score==rhs.score&&lhs.num<rhs.num){
return true;
} else{
return false;
}
}
int main(){
int n,num,score;
scanf("%d",&n);
vector<MyType> vec1;
for(int i=0;i<n;++i){
scanf("%d%d",&num,&score);
vec1.push_back({num,score});
}
sort(vec1.begin(),vec1.end(),compare);
for(int i=0;i<vec1.size();++i){
printf("%d %d\n",vec1[i].num,vec1[i].score);
}
}
成绩排序
#include <bits/stdc++.h>
using namespace std;
struct MyType{
int num;
string name;
int score;
};
bool compare1(MyType lhs,MyType rhs){
if(lhs.score<rhs.score){
return true;
} else if(lhs.score==rhs.score&&lhs.num<rhs.num){
return true;
}else{
return false;
}
}
bool compare2(MyType lhs,MyType rhs){
if(lhs.score>rhs.score){
return true;
} else if(lhs.score==rhs.score&&lhs.num<rhs.num){
return true;
}else{
return false;
}
}
int main(){
int n,score,type;
scanf("%d",&n);
vector<MyType> vec1;
string name;
scanf("%d",&type);
for(int i=0;i<n;++i){
cin>>name;
scanf("%d",&score);
vec1.push_back({i,name,score});
}
if(type){
sort(vec1.begin(),vec1.end(),compare1);
}else{
sort(vec1.begin(),vec1.end(),compare2);
}
for(int i=0;i<vec1.size();++i){
printf("%s %d\n",vec1[i].name.c_str(),vec1[i].score);
}
}