关于next_permutation的用法

来源:互联网 发布:淘宝买处方药 编辑:程序博客网 时间:2024/06/15 00:51

next_permutation是STL中的一个函数,头文件是#include<algorithm>,作用是生成全排列,默认是后一个比前一个大

例如aacc,生成全排列后为:acac  acca  caac caca  ccaa ,根据ASCALL码的顺序排列,加上本身共6个!

函数模型:

bool next_permutation(first, last);

bool next_permutation(first,last,cmp);   cmp为比较函数,自己定义即可。


CODE:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
int len,count=0;
char s[20];
gets(s);    //貌似不能用scanf("%c",s[i]),可用scanf("%s",s)直接输入
len=strlen(s);
sort(s,s+len);
while(next_permutation(s,s+len))
{
puts(s);//同理
count++;
}
printf("%d\n",count+1);
return 0;
}



更多更详细的介绍请进  http://www.cplusplus.com/reference/algorithm/next_permutation/

                                          http://www.slyar.com/blog/stl_next_permutation.html

PS:现阶段只知道这么多,本人只是抛砖引玉,希望各位大神指点指点!!


原创粉丝点击