首页 > 其他 > 详细

“下一个排列”next_permutation的用法

时间:2020-04-14 19:27:50      阅读:77      评论:0      收藏:0      [点我收藏+]

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

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 }

 

“下一个排列”next_permutation的用法

原文:https://www.cnblogs.com/zcr-blog/p/12700010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!