您的位置:首页 > 其它

关于next_permutation的用法

2013-08-21 15:44 337 查看
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:现阶段只知道这么多,本人只是抛砖引玉,希望各位大神指点指点!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: