题解 | #成绩排序#

成绩排序

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);
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-09 11:30
找工作7个月,投了7000封,3段世界五百强实习,才有一个offer,牛油们肯定比我强吧
码农索隆:不对不对不对,实习经历这么厉害,简历也没少投,问题出在哪呢
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 18:05
哈哈哈哈哈感觉朋友找工作的已经疯掉了,直接上图
码农索隆:真老板娘:“我嘞个去,这不我当年的套路吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务