我用递归实现的 1~9 的全排
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int cd(int m[],int num1)
{
int k,l;
l=0;
if(num1==8){
printf("%d%d%d%d%d%d%d%d%d\n",m[0],m[1],m[2],m[3],m[4],m[5],m[6],m[7],m[8]);
return 0;
}
else{
cd(m,num1+1);
l++;
if((num1+l)<=8){
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
cd(m,num1+1);
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
}
l++;
if((num1+l)<=8){
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
cd(m,num1+1);
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
}
l++;
if((num1+l)<=8){
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
cd(m,num1+1);
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
}
l++;
if((num1+l)<=8){
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
cd(m,num1+1);
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
}
l++;
if((num1+l)<=8){
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
cd(m,num1+1);
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
}
l++;
if((num1+l)<=8){
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
cd(m,num1+1);
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
}
l++;
if((num1+l)<=8){
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
cd(m,num1+1);
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
}
l++;
if((num1+l)<=8){
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
cd(m,num1+1);
k=m[num1];
m[num1]=m[num1+l];
m[num1+l]=k;
}
return 0;
}
}
int main()
{
int j,num,i[9];
num=0;
for(j=0;j<9;j++)
i[j]=j+1;
cd(i,num);
}
[此贴子已经被作者于2016-4-19 04:20编辑过]