输入几个无重复的字母,将这几个字母的全排列输出出来.比如输入a,b,c,会输出"abc,acb,bac,bca,cba,cab, 20
2个回答
展开全部
#include<stdio.h>
#include<stdlib.h>
#define N 5
int a[120][N];//a[line][N]
int get_line(int n)
{
int line=n;
while(--n)
line=line*n;
return line;
}
void exchange(int num)
{
int rand_N0,rand_N1,t;
rand_N0=rand()%N;
rand_N1=rand()%N;
t=a[num][rand_N0];
a[num][rand_N0]=a[num][rand_N1];
a[num][rand_N1]=t;
}
int campar(int num0,int num1)
{
int i,times=0;
for(i=0;i<N;i++)
if(a[num0][i]==a[num1][i])
times++;
return times;
}
void main()
{
int i,j,num=0,line,clash=0;
line=get_line(N);
for(i=0;i<line;i++)
for(j=0;j<N;j++)
a[i][j]=j+'a';
while(num<line){
if(clash)
clash=0;
else
{
printf("the %3d line is ",num);
for(i=0;i<N;i++)
printf(" %c ", a[num][i]);
printf("\n");
num++;
}
exchange(num);
for(i=0;i<num;i++){
if(campar(num,i)==N)
clash=1;
}
}
}
#include<stdlib.h>
#define N 5
int a[120][N];//a[line][N]
int get_line(int n)
{
int line=n;
while(--n)
line=line*n;
return line;
}
void exchange(int num)
{
int rand_N0,rand_N1,t;
rand_N0=rand()%N;
rand_N1=rand()%N;
t=a[num][rand_N0];
a[num][rand_N0]=a[num][rand_N1];
a[num][rand_N1]=t;
}
int campar(int num0,int num1)
{
int i,times=0;
for(i=0;i<N;i++)
if(a[num0][i]==a[num1][i])
times++;
return times;
}
void main()
{
int i,j,num=0,line,clash=0;
line=get_line(N);
for(i=0;i<line;i++)
for(j=0;j<N;j++)
a[i][j]=j+'a';
while(num<line){
if(clash)
clash=0;
else
{
printf("the %3d line is ",num);
for(i=0;i<N;i++)
printf(" %c ", a[num][i]);
printf("\n");
num++;
}
exchange(num);
for(i=0;i<num;i++){
if(campar(num,i)==N)
clash=1;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询