C语言-递归做字符串数组元素全排列

#include 
#include 
void swap(char *a, char *b)		{	char tmp;	tmp = *a;	*a = *b;	*b = tmp;	}
int count = 0;
void permutation(char *list, int start, int end, int deep)
{
	printf("start=%d,end=%d, deep=%d into \n", start, end, deep);
	for (int i = start; i <= end; i ++)
	{
		swap(&list[i], &list[start]);
		permutation(list, start + 1, end, deep+1);
		swap(&list[i], &list[start]);
		if(i==end)
		{
			count++;
			printf("%s \n", list);
		}
	}
	printf("start=%d,end=%d, deep=%d out\n", start, end, deep);
}
int main()
{
	char list[]="abc";
	permutation(list, 0, 2, 0);
	printf("%d \n", count);
}
start=0,end=2, deep=0 into 
start=1,end=2, deep=1 into 
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
abc 
start=2,end=2, deep=2 out
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
acb 
start=2,end=2, deep=2 out
abc 
start=1,end=2, deep=1 out
start=1,end=2, deep=1 into 
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
bac 
start=2,end=2, deep=2 out
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
bca 
start=2,end=2, deep=2 out
bac 
start=1,end=2, deep=1 out
start=1,end=2, deep=1 into 
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
cba 
start=2,end=2, deep=2 out
start=2,end=2, deep=2 into 
start=3,end=2, deep=3 into 
start=3,end=2, deep=3 out
cab 
start=2,end=2, deep=2 out
cba 
start=1,end=2, deep=1 out
abc 
start=0,end=2, deep=0 out
10 

 

你可能感兴趣的:(C语言与数据结构)