蓝桥-BASIC-13-数列排序

问题描述
  给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200
输入格式
  第一行为一个整数n。
  第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000。
输出格式
  输出一行,按从小到大的顺序输出排序后的数列。
样例输入
5
8 3 6 4 9
样例输出

3 4 6 8 9

注:练习使用stl的sort

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

struct Comp{
	private:
		bool flag_;
	public:
		Comp(bool flag):flag_(flag){
		}
		bool operator()(const int &a,const int &b){
			return flag_?a>b:b>a;
		}
};

int main()
{
	vector<int> a;
	int n,temp;
	cin>>n;
	for(size_t i = 0;i<n;i++){
		cin>>temp;
		a.push_back(temp);
	} 
	sort(a.begin(),a.end(),Comp(false));
	for(vector<int>::iterator index=a.begin();index!=a.end();index++){
		cout<<*index<<" ";
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值