全排列函数next_permutation

全排列函数next_permutation

next_permutation(begin,end)在algorithm头文件中,begin是数组的头,end是数组的尾
char/int类型:

#include<iostream>
#include<algorithm>   //next_permutation所在的头文件
#include<cstring>
using namespace std;
/*
C++中全排列函数的使用:但它不会输出原始输入的那种排列方式 
*/
int main()
{
	int len;
	char a[1000];
	while(cin>>a)  //输入 
	{
		len=strlen(a);  //求得数组长度 
		while(next_permutation(a,a+len))  //遍历整个数组 
		{
			cout<<a<<endl;   //输出每一种可能 
		}
		memset(a,'\0',sizeof(a));  //初始化 
	} 	
	return 0;
} 

string类型:

#include<iostream>
#include<algorithm>   //next_permutation
#include<cstring>
using namespace std;
/*
C++中全排列函数的使用:但它不会输出原始输入的那种排列方式 
*/
int main()
{
	int len;
	string s;
	while(cin>>s)  //输入 
	{
		len=s.length();  //求得数组长度 
		while(next_permutation(s.begin(),s.end()))  //遍历整个数组 
		{
			cout<<s<<endl;   //输出每一种可能 
		}
	} 	
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_39905917/article/details/84574146