当前位置:

首页 >php

php 实现全排列

< ?php
header('Content-Type: text/html; charset=UTF-8');
class quanpailie
{
	var $startTime = 0;
	var $stopTime = 0;
 
    function get_microtime()
    {
        list($usec, $sec) = explode(' ', microtime());
        return ((float)$usec + (float)$sec);
    }
	function start()
    {
        $this->startTime = $this->get_microtime();
    }
	function stop()
	{
		$this->stopTime = $this->get_microtime();
	}
	function spent()
	{
		return round(($this->stopTime - $this->startTime) * 1000, 3);
	}
	function type($list, $k, $m)
	{
		if($k==$m)
		{
			for($i=0; $i< =$m; $i++)
			{
				echo  $list[$i];
			}
			echo "\n";
		}
		else
		{
			for($i=$k; $i<=$m; $i++)
			{
				$this->swap($list[$k], $list[$i]);
				$this->type($list, $k+1, $m);
				$this->swap($list[$k], $list[$i]);
			}
		}
	}
	public function swap(&$a, &$b)
	{
		$temp=$a;
		$a=$b;
		$b=$temp;
	}
}
$aa=new quanpailie();
$aa->start();
$str = 'abcde';
$aa->type(str_split($str), 0, 2);
$aa->stop();
echo "<br />页面执行时间: ". $aa->spent() . " 毫秒";
?>

本文引用地址: 

上一篇: 下一篇:

发表评论 2 位网友发表了评论  

  1. van 说:

    这样的代码风格我喜欢

    [回复]

  2. van 说:

    就是这个类名 “quanpailie”。。。。看着蛋疼。。

    [回复]

添加新的评论 浏览评论»  

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!:



注意:
1、本站启用了审核机制,你的留言可能稍后才会显示,请不要重复提交,谢谢。
2、留言时的头像是Gravatar提供的服务。想设置的看这里