【无浪】全排列实现之一


选择性强迫症,选择性不能忽略任何一点瑕疵。


在网上下了 数据结构 朱仲涛版,实现了它上面的代码,这次是全排列。


#include<iostream>
using namespace std;

void SWAP(char &i,char &j,char &temp)
{
		temp=i;
	    i=j;
	    j=temp;
}

void perm(char*list,int i,int n)
{
	int j;
	char temp;
	if(i==n)
	{	
		for(j=0;j<n;j++)
		{
			cout<<list[j]<<" ";
		}cout<<list[j]<<endl;

	}
	else
	{
		for(j=i;j<=n;j++)
		{      
			SWAP(list[i],list[j],temp);
			perm(list,i+1,n);
			SWAP(list[i],list[j],temp);
			//定义两个SWAP的原因是要换回来,数组位置基本没变过。
			//j==n的用处仅仅在于其中一个与最后一个交换
			//i和j的过程(每个END之前都有一次交换,一种是有意义的,都是复原数组,另一种是没意义的)
        	/*perm(list,0,5);
			       perm(list,1,5);
			              perm(list,2,5);
			                     perm(list,3,5);
			                           perm(list,4,5);
			                                 perm(list,5,5);(输出a b c d e f)
											 END;
			                           i=4<->j=5;
									         perm(list,5,5);输出(a b c d f e)
									         END;
									   End;
								 i=3<->j=4;
								       permu(list,4,5);(之后这个式子直接省略,以最后两个交换一次代替。)
								             perm(list,5,5);(输出a b c e d f)
									         END;
								   	   i=4<->j=5;
									         perm(list,5,5);(输出a b c e f d);
									         END;
								       End;
								 i=3<->j=5;
								       perm(list,4,5);
									         perm(list,5,5);(输出a b c f e d)
									         perm(list,5,5);(输出a b c f d e)
									   End;
								 End;
						  i=2<->j=3;
						         permu(list,3,5);
								       同上再来一次。
						  i=2<->j=4;
						         permu(list,3,5);
								       同上再来一次。
						  i=2<->j=5;
						         permu(list,3,5);
								       同上再来一次。
						  End;
				   i=1<->j=1;
				          perm(list,2,5);
						  同上再来一次;
				   i=1<->j=2;
				          perm(list,2,5);
						  同上再来一次;
				   i=1<->j=3;
				          perm(list,2,5);
						  同上再来一次;
				   i=1<->j=4;
				          perm(list,2,5);
						  同上再来一次;
				   i=1<->j=5;
				          perm(list,2,5);
						  同上再来一次;
				   End;

*/                    
		}
	}
}

int main()
{
	char a[]={'a','b','c','d','e','f'};
	perm(a,0,5);
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值