Skip to content

Instantly share code, notes, and snippets.

@Abreto
Created November 1, 2012 15:40
Show Gist options
  • Save Abreto/3994439 to your computer and use it in GitHub Desktop.
Save Abreto/3994439 to your computer and use it in GitHub Desktop.
递归生成1~n的排列
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