Files
python/C++专题课程/STL基础知识/STL-10-全排列.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

32 lines
996 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
/*
知识点内容:STL 全排列函数详解
从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列。
当m=n时所有的排列情况叫全排列。如果这组数有n个,那么全排列数为n!个。
文档内容参考:
https://www.cnblogs.com/aiguona/p/7304945.html
*/
using namespace std;
int main() {
int arr[] = {3, 2, 1};
cout << "用prev_permutation对3 2 1的全排列" << endl;
do {
cout << arr[0] << ' ' << arr[1] << ' ' << arr[2] << '\n';
} while (prev_permutation(arr, arr + 3)); ///获取上一个较大字典序排列,如果3改为2,只对前两个数全排列
int arr1[] = {1, 2, 3};
cout << "用next_permutation对1 2 3的全排列" << endl;
do {
cout << arr1[0] << ' ' << arr1[1] << ' ' << arr1[2] << '\n';
} while (next_permutation(arr1, arr1 + 3)); ///获取下一个较大字典序排列,如果3改为2,只对前两个数全排列
///注意数组顺序,必要时要对数组先进行排序
return 0;
}