next_permutation()
全排列函数 , 作用是输出所有比当前排列大的排列(顺序从小到大排)
string str;
cin>>str;
while(next_permutation(str.begin(),str.end()))
cout<<str<<endl;
或者
char str[100];
cout<<"请输入原序列:";
cin>>str;
int len=strlen(str);
while(next_permutation(str,str+len)){
cout<<str<<endl;
}
perv_permutation()
作用是输出比当前排列小的排列(顺序从大到小)
string str;
cout<<"请输入原序列:" ;
cin>>str;
while(prev_permutation(str.begin(),str.end()))
cout<<str<<end;
或者
char str[100];
cout<<"请输入原序列:" ;
cin>>str;
int len=strlen(str);
while(prev_permutation(str,str+len))
{
cout<<str<<endl;
}
如果要得到几个数的全排列,可对上方函数进行一些更改
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int cmp(char a,char b)
{
return a<b;
}
int main()
{
char str[100];
int sum=0;
cout<<"请输入序列:";
cin>>str;
int len=strlen(str);
sort(str,str+len,cmp);
while(next_permutation(str,str+len))
{
cout<<str<<endl;
sum++;
}
cout<<sum<<endl;
return 0;
}