next_permutation指下一个排列,prev_permutation指前一个排列
next_permutation在进行全排列时必须保证待排序数组升序,这样才能得到全部n!种排列方式。同理,prev_permutation在进行全排列时必须保证待排序数组降序,这样才能得到全部n!种排列方式。
next_permutation得到的排列使得后一个排列一定大于前一个排列,如果此时的排列已是最后一个排列,则返回false
prev_permutation得到的排列使得后一个排列一定小于前一个排列,如果此时排列已是最后一个排列,返回false
//next_permutation,prev_permutation的运用
#include<bits/stdc++.h> //万能头文件,包含c++所有头文件
using namespace std;
int main()
{
char s[100];
while(scanf("%s",s)==1)
{
int len=strlen(s);
sort(s,s+len);
do{
printf("%s\n",s);
}while(next_permutation(s,s+len));
printf("\n");
sort(s,s+len,greater<char>());
do{
printf("%s\n",s);
}while(prev_permutation(s,s+len));
}
return 0;
}