phpstudy_PHP 实现的字典序排列算法

时间:2013-11-11  来源:感谢短信  阅读:
感谢 bird 告诉我 $_POST[] 的用法。代码如下:

  if ($_POST["perdata"] == "")
{
$_POST["perdata"] = "1 2 3 4";
}
$data = chop (trim ($_POST["perdata"]));
$a = explode (" ", $data);
sort ($a);
$data = implode (" ", $a);
?>
function nextpermu (&$c)
{
$s = sizeof ($c);
$i = $s - 1;
while ($i > 0)
{
if ($c[$i] > $c[$i-1])
{
$j = $s-1;
while ($c[$j] <= $c[$i-1])
$j--;
$t = $c[$i-1];
$c[$i-1] = $c[$j];
$c[$j] = $t;
//echo $i."-".$j."
";
for ($j=$s-1; $i < $j; $i , $j--)
{
$t = $c[$i];
$c[$i] = $c[$j];
$c[$j] = $t;
}
return true;
}
$i--;
}
for ($i = 0, $j=$s-1; $i < $j; $i , $j--)
{
$t = $c[$i];
$c[$i] = $c[$j];
$c[$j] = $t;
}
return false;
}
?>


排列-字典法










当前元素:




序号
排列

$num = 1;
do
{
?>




$num ;
}
while (nextpermu ($a));
?>



phpstudy_PHP 实现的字典序排列算法

http://m.bbyears.com/zhufuduanxin/1895.html

推荐访问:phpstorm php面试题 php数组转字符串 php教程
相关阅读 猜你喜欢
本类排行 本类最新