Created
November 1, 2012 15:40
-
-
Save Abreto/3994439 to your computer and use it in GitHub Desktop.
递归生成1~n的排列
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void swap(int *a, int *b) | |
{ | |
int t = *a; | |
*a = *b; | |
*b = t; | |
} | |
void permutation(int *S, int start, int end, int *A, int cur) | |
{ | |
int i = 0; | |
if(start == end) // 递归边界 | |
{ | |
*(A+cur) = *(S+start); | |
for(i = 0;i < end+1;i++) printf("%d ", *(A+i)); | |
printf("\n"); | |
} | |
else | |
{ | |
for(i = start;i < end+1;i++) | |
{ | |
*(A+cur) = *(S+i); | |
swap(S+start, S+i); | |
permutation(S, start+1, end, A, cur+1); | |
swap(S+start, S+i); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment