假如给定一字符串“ABCDE” 编写一段程序输出它的全排列,代码如下:
void DoPermute(char* in, char* out,char* used,int length,int recurse_level)
{
int i;
if(recurse_level == length)
{
printf("%s/n",out); //print permutation
return;
}
//recursive case
for(i = 0; i < length; i++)
{
if(used[i]) //if used, skip to next letter
continue;
out[recurse_level] = in[i]; //put current letter in output
used[i] = 1;
DoPermute(in, out,used,length,recurse_level + 1);
used[i] = 0;
}
}
void Permute(char* p_string)
{
permute_count = 0;
int length = strlen(p_string) + 1;
char* out_put = new char[length];
memset(out_put,0,length);
char* used = new char[length];
memset(used,0,length - 1);
DoPermute(p_string,out_put,used,length - 1, 0);
delete out_put;
delete used;
}
int main(int argc, char* argv[])
{
char s_permute[] = "abcde";
Permute(s_permute); //call fully permute
return 0;
}