stl 生产全排列 next_permutation

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n,p[10];
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&p[i]);
sort(p,p+n);//排序之后才能使用;
do{
for(int i=0;i<n;i++)
printf("%d",p[i]);
printf("\n");
}while(next_permutation(p,p+n));
return 0;
}

时间: 2024-08-01 16:58:19

stl 生产全排列 next_permutation的相关文章

枚举所有排列-STL中的next_permutation

枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 #include<cstdio> #include<algorithm> using namespace std; int main() { int n,p[10]; scanf("%d",&n); for(int i=0;i<n;i++) scan

【STL】全排列生成算法:next_permutation

C++/STL中定义的next_permutation和prev_permutation函数是非常灵活且高效的一种方法,它被广泛的应用于为指定序列生成不同的排列. next_permutation函数将按字母表顺序生成给定序列的下一个较大的排列,直到整个序列为降序为止. prev_permutation函数与之相反,是生成给定序列的上一个较小的排列. 所谓“下一个”和“上一个”,举一个简单的例子: 对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,c比b

STL中关于全排列next_permutation以及prev_permutation的用法

这两个函数都包含在algorithm库中.STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. 一.函数原型 首先我们来看看这两个函数的函数原型: next_permutation: 1 template< class BidirIt >bool next_permutation( BidirIt first, BidirIt last ); 2 template< class BidirIt, class Compare

全排列 ( next_permutation)

SLT: C++的STL有一个函数可以方便地生成全排列,这就是next_permutation 在C++ Reference中查看了一下next_permutation的函数声明: #include <algorithm>bool next_permutation( iterator start, iterator end ); The next_permutation() function attempts to transform the given range of elements [

组合数学 + STL --- 利用STL生成全排列

Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4730    Accepted Submission(s): 2840 Problem Description Now our hero finds the door to the BEelzebub feng5166. He op

STL::next_permutation();

next_permutation()可以按字典序生成所给区间的全排列. 在STL中,除了next_permutation()外,还有一个函数prev_permutation(),两者都是用来计算排列组合的函数.前者是求出下一个排列组合,而后者是求出上一个排列组合.所谓“下一个”和“上一个”,书中举了一个简单的例子:对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,c比b大,它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a,

STL中的next_permutation

给定一个数组a[N],求下一个数组. 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 ..... 在STL中就有这个函数: 1.参数是(数组的第一个元素,数组的末尾),注意这是前闭后开区间,(a,a+n) 2.返回值是bool型,表示这个数组是不是最后一个元素. 3.这个函数不仅可以实现n个互异的数的全排列,也能够生成组合数.如1 2  3 3 4 4 5 6 6这样数组的全排列. 4.第一个数组是升序排列,最后一个数组是降序排列. int a[] = {1,2,4,4}; do

关于全排列 next_permutation() 函数的用法

这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下面的代码可产生1~n的全排列. #include <stdio.h> #include <algorithm> using namespace std; int main(){ int n; while(scanf("%d",&n)&&n){

c++ STL中的next_permutation

default (1) template <class BidirectionalIterator> bool next_permutation (BidirectionalIterator first, BidirectionalIterator last); custom (2) template <class BidirectionalIterator, class Compare> bool next_permutation (BidirectionalIterator f