全排列

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

int main(){
	vector<int> numbers = {1,2,3};
	// 从大到小排序 
	sort(numbers.begin(),numbers.end(),[](const int & a,const int & b){
	return a>b;
	});
	do
	{
		for(auto i:numbers){
			cout << i++ << " ";
		}
		cout << endl;
	}while(prev_permutation(numbers.begin(),numbers.end()));
	
	cout << "-------------------------------------" << endl;
	// 从小到大排序 
	sort(numbers.begin(),numbers.end());
	do
	{
		for(auto i:numbers){
			cout << i++ << " ";
		}
		cout << endl;
	}while(next_permutation(numbers.begin(),numbers.end()));

	return 0;	
}

输出

3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
-------------------------------------
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
posted @ 2024-03-23 16:16  Mask_2022  阅读(23)  评论(0)    收藏  举报