# include <stdio.h>
/**
* 时间:2016年6月5日 11:27:44
* 内容:使用递归来实现排列组合
*/
# define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t))
void Permutations(char*,int,int);
void Info(char*,int);
int count=0;
int main(void)
{
char a[]="abcde";
int length=5;
Info(a,length);
Permutations(a,0,length);
printf("总共有%d种排列方式\n",count);
return 0;
}
/**
* 递归排序
* @param a 未排序的数组
* @param Start 初始角标
* @param End 终止角标
*/
void Permutations(char* a,int Start,int End)
{
char temp;
if (Start==End)
{
printf("%s\n", a);
count++;
}
else
{
for (int i=Start;i<End;i++)
{
SWAP(a[i],a[Start],temp);
Permutations(a,Start+1,End);
SWAP(a[i],a[Start],temp);
}
}
/* // a开头的,后面bc的所有排列
SWAP(a[0],a[0],temp);
Permutations(a,1,2);
SWAP(a[0],a[0],temp);
// b开头的,后面ac所有排列
SWAP(a[0],a[1],temp);
Permutations(a,1,2);
SWAP(a[0],a[1],temp);
// c开头的,后面ab的所有排列
SWAP(a[0],a[2],temp);
Permutations(a,1,2);
SWAP(a[0],a[2],temp);
*/
}
void Info(char*a,int n)
{
for (int i=0;i<n;i++)
{
printf("%c ",a[i]);
}
printf("\n");
}
递归排序
最新推荐文章于 2023-09-14 20:49:02 发布