您的位置:首页 > 其它

全排列

2016-06-20 22:17 330 查看
http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/

http://www.cs.utexas.edu/users/djimenez/utsa/cs3343/lecture25.html

http://stackoverflow.com/questions/9148543/program-to-print-permutations-of-given-elements

http://www.cnblogs.com/guxuanqing/p/5904092.html

非常感谢作者!





针对含有重复串的更精简的方法:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main ()
{
char str[] = "ABA";
int len = strlen (str);
void permute (char *, int, int);
permute (str, 0, len - 1);
return 0;
}
void permute (char *A, int l, int r)
{
void swap (char *, int, int);
int i;
if (l == r) // Only 1 char left, print the permutation
{
printf ("%s\n", A);
}
else
{
for (i = l; i <= r; i++)
{
if (i == l)
permute (A, l + 1, r);
else if (A[l] == A[i])
{
continue;
}
else
{
swap (A, l, i);
permute (A, l + 1, r);
swap (A, l, i);
}
}
}
}
void swap (char *A, int l, int i)
{
int tmp = A[l];
A[l] = A[i];
A[i] = tmp;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: