PHP写杨辉三角实例代码

复制代码 代码如下:

  <?php

  //杨辉三角

  for ($i=6;$i >= 0;$i--)

  {

  for ($j=$i;$j <= 6;$j++)

  {

  if ($j <= 6-1)

  {

  echo "<b>a</b>";

  }else

  {

  echo "<br />";

  }

  }

  }

  ?>

  PHP打印杨辉三角自定义

  

复制代码 代码如下:

  <form method="post" action="<?php echo($PHP_SELF); ?>">

  输入杨辉三角的阶数:<input type="text" name="givenlines" size="5">

  <input type="submit" name="submit" value="打印杨辉三角形">

  </form>

  <?php

  function yanghui($line)

  {

  echo "<table>";

  for($i=1;$i<=$line;$i++)

  {

  echo "<tr>";

  for($j=1;$j<=$i;$j++)

  {

  $yh[$i][1]=1;

  if ($i==$j) $yh[$i][$j]=1;

  else $yh[$i][$j]=$yh[$i-1][$j-1]+$yh[$i-1][$j];

  echo "<td width=40> <font color=#0000FF>";

  echo $yh[$i][$j];

  echo "</font> </td>";

  }

  echo "</tr>";

  }

  echo "</table>";

  }

  if($_POST['submit']) yanghui($_POST['givenlines']);

  ?>