“下一个排列”next_permutation的用法

有一种枚举所有排列的方法是从最小排列开始不断求下一个排列。

C++ STL库里已经有一个帮你写好的求下一个排列的函数“next_permutation”。

具体用法看下面的代码:

 1 #include <cstdio>
 2 #include <algorithm>//包含sort和next_permutation 
 3 using namespace std;
 4 int n, a[100];
 5 int main() {
 6     scanf("%d", &n);
 7     for (int i = 0; i < n; i++) scanf("%d", &a[i]);
 8     sort(a, a + n);
 9     do {
10         for (int i = 0; i < n - 1; i++) printf("%d ", a[i]);
11         printf("%d\n", a[n - 1]);
12     } while (next_permutation(a, a + n)); 
13     return 0;
14 }

猜你喜欢

转载自www.cnblogs.com/zcr-blog/p/12700010.html