permutation.php 的源代码:


<?php
  if ($_POST["perdata"] == "")
  {
    $_POST["perdata"] = "1 2 3 4";
  }

  $data = chop (trim ($_POST["perdata"]));
  $a = explode (" ", $data);
  sort ($a);
  $data = implode (" ", $a);
?>

<?php
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."<br>";
      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;
}

?>
<html>
<head>
  <meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8">
  <title>Permutation Algorithm</title>
</head>
<body>
  <form action="permutation.php" method="post">
  <table>
    <tr>
      <td><input type="text" name="perdata" value="<? echo $data; ?>"></td>
      <td><input type="submit" value="Generate Permutations"></td>
    </tr>
  </table>
  </form>

  <table width="60%">
    <tr>
      <th width="50" bgcolor="yellow">Order</th>
      <th bgcolor="EEEEFF">the Permutation</th>
    </tr>
<?php
  $num = 1;

  do
  {
?>
    <tr>
      <td align="center"><? echo $num; ?> </td>
      <td><? echo implode (" ", $a); ?></td>
    </tr>
<?php
    $num++;
  }
  while (($num < 5000) and nextpermu ($a));
?>
  </table>
</body>
</html>