用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等。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-12
#include <stdio.h>

int main(void)
{
int i,j,k,l;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(j!=i)
for(k=0;k<4;k++)
if(k!=i && k!=j)
for(l=0;l<4;l++)
if(l!=k && l!=i && l!=j)
printf("%c%c%c%c\n",i+'A',j+'A',k+'A',l+'A');
return 0;
}本回答被网友采纳
相似回答