#include<stdio.h>
int count = 0;//数组中元素的个数
//全排列函数
void perm (char alph[],int start)
{
int i;
char temp;
if(count-start==2)//剩下两个元素时停止递归
{
for(i=0;i<count;i++)
printf("%c",alph[i]);
putchar('\n');
temp=alph[count-2];alph[count-2]=alph[count-1];alph[count-1]=temp;//后两个元素交换
for(i=0;i<count;i++)
printf("%c",alph[i]);
putchar('\n');
temp=alph[count-2];alph[count-2]=alph[count-1];alph[count-1]=temp;//需要把两个元素再交换回来,避免重复
}
else
{
for(i=start;i<count;i++)
{
temp=alph[start];alph[start]=alph[i];alph[i]=temp;
perm(alph,start+1);
temp=alph[start];alph[start]=alph[i];alph[i]=temp;
}
}
}
int main()
{
char alph[]={'a','b','c','d'};
count = sizeof(alph)/sizeof(char);
perm(alph,0);
return 0;
}
全排列
最新推荐文章于 2024-08-04 08:42:07 发布