递归算法通过穷举实现全排列
#include<stdio.h>
void swap(int *p, int i, int j)
{
int temp=0;
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
void comb(int *p,int length,int N)
{
int *p2 = p,k=length-1;
if (length==1)
{
for (int i = 0; i <=N-1; i++)
{
printf("%d ", p[i]);
}
printf("\n");
cunt++;
}
else
{
for (int i=0;i<length;i++)
{
swap(p2, i,k );
comb(p2,length-1,N);
swap(p2, k, i);
}
}
}
//static int cunt = 0;
int main()
{
int N;
int a[10] = { 1, 2, 3, 4 ,5,6,7,8,9,10};
N=10;
comb(a, 10,N);
printf("%d\n", cunt);
return 0;
}