【优先队列orSort】清华大学考研复试上机——成绩排序

题目描述   题目链接->点击打开链接

查找和排序

题目:输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,相同成绩
      都按先录入排列在前的规则处理。

   例示:
   jack      70
   peter     96
   Tom       70
   smith     67

   从高到低  成绩            
   peter     96    
   jack      70    
   Tom       70    
   smith     67    

   从低到高

   smith     67  

   Tom       70    
   jack      70    
   peter     96      

 

输入描述:

输入多行,先输入要排序的人的个数,然后输入排序方法0(降序)或者1(升序)再分别输入他们的名字和成绩,以一个空格隔开

输出描述:

按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开

示例1

 

输入

3
0
fang 90
yang 50
ning 70

输出

fang 90
ning 70
yang 50

示例2

输入

28
1
qhsq 15
ozslg 79
ncttmtsphb 71
a 39
eeiuyzsj 34
nmlrokx 21
pjizylo 90
ec 45
f 12
sh 31
fm 25
ptprphubqk 29
wxdiwv 0
uhlcpjtxad 60
w 20
zwktbpun 70
efzfkf 69
v 31
rsnrgtl 73
lhdo 76
wt 56
mcdwd 14
ydrnoyd 37
gmlfds 76
zx 1
dqx 98
gz 90
kvbzrwrrjj 13

输出

wxdiwv 0
zx 1
f 12
kvbzrwrrjj 13
mcdwd 14
qhsq 15
w 20
nmlrokx 21
fm 25
ptprphubqk 29
sh 31
sh 31
eeiuyzsj 34
ydrnoyd 37
a 39
ec 45
wt 56
uhlcpjtxad 60
efzfkf 69
zwktbpun 70
ncttmtsphb 71
rsnrgtl 73
lhdo 76
lhdo 76
ozslg 79
pjizylo 90
pjizylo 90
dqx 98

方法1:使用优先队列解决,设计两个按成绩升序和降序的学生数组,每次将学生信息添加到优先队列,再输出即可。

#include <iostream>
#include<queue>
#include <cstring>
#include <map>

using namespace std;

struct stu1{
	string name;
	int score;
	bool operator<(const stu1&rhs)const{
		return score>rhs.score;//优先输出成绩低的
	}
};
struct stu2{
	string name;
	int score;
	bool operator<(const stu2&rhs)const{
		return score<rhs.score;//优先输出成绩高的
	}
};
int main() {
    stu1 st1;
    stu2 st2;
    map<int,string>mp1;
    map<int,string>mp2;
    string s;
    int score;
    int n,flag;
    while(cin>>n>>flag){
        if(flag==1){//==1 升序
            priority_queue<stu1> q1;
            for (int i = 0; i < n; i++) {
                cin >> s;
                cin >> score;
                getchar();
                st1.score = score;

                mp1.insert(pair<int,string>(score,s));
                q1.push(st1);
            }
            while (!q1.empty()) {
                st1 = q1.top();
                cout<<mp1[st1.score]<<" ";
                cout << st1.score << endl;
                q1.pop();
            }
        }
        else if(flag==0){//==0 降序
            priority_queue<stu2> q2;
            for (int i = 0; i < n; i++) {
                cin >> s;
                cin >> score;
                getchar();
                st2.score = score;

                mp2.insert(pair<int,string>(score,s));
                q2.push(st2);
            }
            while (!q2.empty()) {
                st2 = q2.top();

                cout<<mp2[st2.score]<<" ";
                cout << st2.score << endl;
                q2.pop();
            }
        }
    }

    return 0;
}

做完后发现也有大佬用vector做出的,也是甚为简单->点击打开链接,于是自己也手写了下面代码:

 

方法2:用vector数组存储<string,int>的pair,然后自定义重写两个升序和降序的排序方法,使用#include<algorithm>的sort函数按照自定义排序方法排序。最后循环遍历vector数组即可(以下代码总结了常见的简单的2种vector数组遍历方法):

#include <iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;

vector<pair<string,int> >vc;//vc数组存放的是string和int组成的pair 

int cmpbyAsc(const pair<string,int>&x,const pair<string,int>&y){//升序 
	return x.second<y.second;
}
int cmpbyDesc(const pair<string,int>&x,const pair<string,int>&y){//降序 
	return x.second>y.second;
}
int main(int argc, char** argv) {
	int n,flag;
	string name;
	int grade;
	while(cin>>n>>flag){
		for(int i=0;i<n;i++){//输入name和grade 
			cin>>name>>grade;
			vc.push_back(make_pair(name,grade));
		}
		if(flag==1){//1为升序 
			sort(vc.begin(),vc.end(),cmpbyAsc);//cmpbyAsc后面不需要带参数 
		}
		else{//0为降序 
			sort(vc.begin(),vc.end(),cmpbyDesc);
		}
		//vector数组的两种简单遍历方法 
		for(auto e:vc){//使用C++11新特性遍历vector 
            cout<<e.first<<" "<<e.second<<endl;  
        }
        /*for(size_t i=0;i<vc.size();i++){
        	cout<<vc[i].first<<" "<<vc[i].second<<endl;
		}*/
	}
	return 0;
}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值