c语言字母全排列组合,几种全排列的算法(C语言兑现)

几种全排列的算法(C语言实现)

/*

* 几种排列组合的算法

*/

#include

int a[20];

int n;

//打印数组

void showArray(int *a)

{

int i;

for(i=1;i<=n;i++)

printf("%d",a[i]);

printf("\n");

}

//翻转法

void overturn()

{

int i,temp,temp1,temp2,j;

int b[20];

for(i=1;i<=n;i++)

*(b+n-i+1)=*(a+i);

showArray(b);

for(i=1;i<=n;i++){

//判断第一个数是否为1

if(i==1 && b[i]!=i){

temp=b[i];

for(j=1;j

b[j]=b[j+1];

b[j]=temp;

showArray(b);

i=0;

continue;

}

//判断第二个数是否为1

if(i==2 && b[i]!=i){

temp1=b[i];

temp2=b[i-1];

for(j=1;j+2<=n;j++)

b[j]=b[j+2];

b[j]=temp1;

b[j+1]=temp2;

showArray(b);

i=0;

continue;

}

//判断第三个数是否为1

if(i==3 && b[i]!=i){

temp=b[i];

temp1=b[i-1];

temp2=b[i-2];

for(j=1;j+3<=n;j++)

b[j]=b[j+3];

b[j++]=temp;

b[j++]=temp1;

b[j]=temp2;

showArray(b);

i=0;

continue;

}

}

}

//换位法

void changeSite()

{

int i,temp,temp1;

int b[20],max=0;

int dir[20]={-1,-1,-1,-1,-1,-1};

for(i=1;i<=n;i++)

*(b+i)=*(a+i);

showArray(b);

while(1){

max=0;

b[max]=0;

//寻找最大的活结点

for(i=1;i<=n;i++){

if(i+dir[i]>0 && i+dir[i]<=n && b[i]>b[i+dir[i]])

max=b[i]>b[max]?i:max;

}

if(max==0)

break;

//交换位置和方向

temp=b[max];

b[max]=b[max+dir[max]];

b[max+dir[max]]=temp;

temp1=dir[max+dir[max]];

dir[max+dir[max]]=dir[max];

dir[max]=temp1;

//改变比活结点大的数的方向

for(i=1;i<=n;i++)

if(b[i]>temp)

dir[i]=-dir[i];

showArray(b);

}

}

//序数法(排出的序列为有序的)

//str:待排序的字符序列 n:首字符下标 len:字符串长度

void ordinal(char * str,int n,int len)

{

int i;

char temp;

if(n==len)

puts(str);

for(i=n;i

temp=str[i];

str[i]=str[n];

str[n]=temp;

ordinal(str,n+1,len);

temp=str[i];

str[i]=str[n];

str[n]=temp;

}

}

void main()

{

int i;

char str[20];

for(i=1;i<=4;i++)

a[i]=i;

a[0]=n=4;

printf("对1234这四个数进行全排列\n");

printf("翻转法:\n");

overturn();

printf("换位法:\n");

changeSite();

printf("序数法:\n");

for(i=0;i

str[i]=a[i+1]+'0';

str[i]=0;

ordinal(str,0,n);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值