Home >php教程 >PHP源码 >php输出字符串全排列

php输出字符串全排列

PHP中文网
PHP中文网Original
2016-05-25 17:07:451549browse

php代码

function output($temp,$level)
 {
	for($i=0;$i<$level;$i++)
	{
		echo $temp[$i];
	}
	echo &#39;<br>&#39;;
	
 } 
 
  //产生全排列  递归参数传递一定要注意
 function quanpai($arr,$flag,$level,$num,$temp)
 {
	
	if($level>=$num) {output($temp,$num);return;}
		for($i=0;$i<$num;$i++)
		{
			if($flag[$i]==0)
			{
				$temp[$level]=$arr[$i];
				$flag[$i]=1;
				//if($level==2 && $i==3) {echo $temp[$i];exit;}
				quanpai($arr,$flag,$level+1,$num,$temp);
				$flag[$i]=0;
			}
		}
 }
 
 $arr=array(1,2,3,4);
 $len=count($arr);
 $i=0;
 $flag=array();
 $temp=array();
 while($i<$len){$flag[$i]=0;$i++;}
 
 quanpai($arr,$flag,0,$len,$temp);
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP生成树Next article:PHP实现各种经典算法