题解 | #成绩排序#
成绩排序
http://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
vector<pair<string,int>>d;
vector<pair<string,int>>g;
for(int i=0;i<a;i++)
{
string e;
int f;
cin>>e;
cin>>f;
d.push_back(make_pair(e, f));
g.push_back(make_pair(e, f));
}
for(int i=0;i<a-1;i++)
{
for(int j=i+1;j<a;j++)
{
if(d[i].second>d[j].second)
{
swap(d[i],d[j]);
}
}
}
if(b==0)
{
for(int i=0;i<a-1;i++)
{
for(int j=i+1;j<a;j++)
{
if(d[i].second==d[j].second)
{
if(find(g.begin(),g.end(),pair<string,int>(d[i].first,d[i].second))<find(g.begin(),g.end(),pair<string,int>(d[j].first,d[j].second)))
{
swap(d[i],d[j]);
}
}
}
}
for(int i=a-1;i>=0;i--)
{
cout<<d[i].first<<' '<<d[i].second<<endl;
}
}
if(b==1)
{
for(int i=0;i<a-1;i++)
{
for(int j=i+1;j<a;j++)
{
if(d[i].second==d[j].second)
{
if(find(g.begin(),g.end(),pair<string,int>(d[i].first,d[i].second))>find(g.begin(),g.end(),pair<string,int>(d[j].first,d[j].second)))
{
swap(d[i],d[j]);
}
}
}
}
for(int i=0;i<a;i++)
{
cout<<d[i].first<<' '<<d[i].second<<endl;
}
}
}
return(0);
}
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
vector<pair<string,int>>d;
vector<pair<string,int>>g;
for(int i=0;i<a;i++)
{
string e;
int f;
cin>>e;
cin>>f;
d.push_back(make_pair(e, f));
g.push_back(make_pair(e, f));
}
for(int i=0;i<a-1;i++)
{
for(int j=i+1;j<a;j++)
{
if(d[i].second>d[j].second)
{
swap(d[i],d[j]);
}
}
}
if(b==0)
{
for(int i=0;i<a-1;i++)
{
for(int j=i+1;j<a;j++)
{
if(d[i].second==d[j].second)
{
if(find(g.begin(),g.end(),pair<string,int>(d[i].first,d[i].second))<find(g.begin(),g.end(),pair<string,int>(d[j].first,d[j].second)))
{
swap(d[i],d[j]);
}
}
}
}
for(int i=a-1;i>=0;i--)
{
cout<<d[i].first<<' '<<d[i].second<<endl;
}
}
if(b==1)
{
for(int i=0;i<a-1;i++)
{
for(int j=i+1;j<a;j++)
{
if(d[i].second==d[j].second)
{
if(find(g.begin(),g.end(),pair<string,int>(d[i].first,d[i].second))>find(g.begin(),g.end(),pair<string,int>(d[j].first,d[j].second)))
{
swap(d[i],d[j]);
}
}
}
}
for(int i=0;i<a;i++)
{
cout<<d[i].first<<' '<<d[i].second<<endl;
}
}
}
return(0);
}