查找和排序
题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
都按先录入排列在前的规则处理。
示例:
jack 70
peter 96
Tom 70
smith 67
从高到低 成绩
peter 96
jack 70
Tom 70
smith 67
从低到高
smith 67
jack 70
Tom 70
peter 96
#include<bits/stdc++.h>
using namespace std;
struct student{
string name;
int score;
int wz;
bool operator <(const student&B)
{
if(this->score == B.score)
return this->wz < B.wz;
return this->score < B.score;
}
};
bool rule1 (const student&A,const student&B)
{
if(A.score == B.score)
return A.wz < B.wz;
return A.score < B.score;
}
bool rule2 (const student&A,const student&B)
{
if(A.score == B.score)
return A.wz < B.wz;
return A.score > B.score;
}
int main(){
int n ;
vector<student> vec;
while(cin>>n){
vec.clear();
int label = 0;
cin >> label;
for(int i = 0;i<n;i++){
student s;
cin>>s.name>>s.score;
s.wz = i;
vec.push_back(s);
}
sort(vec.begin(),vec.end());
if(label){
sort(vec.begin(),vec.end(),rule1);
}
else{
sort(vec.begin(),vec.end(),rule2);
}
for(int i = 0;i<n;i++)
cout<<vec[i].name<<" "<<vec[i].score<<endl;
}
return 0;
}