當前位置:編程學習大全網 - 編程軟體 - 用C語言編程:打印由字符A、B、C、D組成的所有全排列。

用C語言編程:打印由字符A、B、C、D組成的所有全排列。

#include<stdio.h>

#include<string.h>char a[20];

int lenth;

long count=0;void main()

{void move(int,int);

int i,j=0;

printf("input:");gets(a);

lenth=strlen(a);

for(i=0;i<lenth;i++)

move(j,i);//move a[i] to the front of a[j];

printf("\ntotal=%d\n",count);

}

void move(int here,int which)//move a[which] to the front of a[here];

{char b[20];

char temp;

int m,n;

if(here<lenth-1)

{if(here!=which)

{for(m=0;m<lenth;m++)

b[m]=a[m];

temp=a[which];

for(m=which;m>here;m--)

a[m]=a[m-1];

a[m]=temp;

}

for(n=here+1;n<lenth;n++)

move(here+1,n);

if(here!=which)

for(m=0;m<lenth;m++)

a[m]=b[m];

}

else

{printf("%-10s",a);

count++;}

} 運行程序時輸入ABCD即可本程序可以輸入任意字符序列。如12345等。

  • 上一篇:UG8.0編程怎麽設安全高度?
  • 下一篇:想學習java要註意什麽?
  • copyright 2024編程學習大全網