简明现代魔法 -> PHP服务器脚本 -> PHP打印杨辉三角
PHP打印杨辉三角
2009-10-03
程序运行演示
输出给定阶数的杨辉三角形
程序代码
<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']);
?>

