algorithm头文件下的next_permutation()

next_permutation给出一个序列在全排列中的下一个序列

举例

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[10]={123};
	do{
		cout<<a[0]<<a[1]<<a[2]<<endl;
	}while(next_permutation(a,a+3));
	return 0;
 } 

输出结果

123
132
213
231
312
321

注意:

1若第九行改为

while(next_permutation(a+1,a+3))

则只对3个数中的后两个数全排列

输出结果

123
132

2.next_permutation()在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。
如果数组num初始化为2,3,1,那么输出就变为了:

213
231
312
321
发布了94 篇原创文章 · 获赞 193 · 访问量 5382

猜你喜欢

转载自blog.csdn.net/weixin_45884316/article/details/104198398