C 언어 - 문자열 배열 요 소 를 재 귀적 으로 배열 합 니 다.
1678 단어 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
", 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
", list);
}
}
printf("start=%d,end=%d, deep=%d out
", start, end, deep);
}
int main()
{
char list[]="abc";
permutation(list, 0, 2, 0);
printf("%d
", 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