张银的博客


Eat to live, but do not live to eat.

导航

PHP打印杨辉三角

Posted on 2009-04-23 19:11  张银  阅读(1444)  评论(0编辑  收藏  举报
<html>
<head>
<title>杨辉三角形</title>
</head>
<body>

<p align="center">输出给定阶数的杨辉三角形</p>
<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']);
?>

</body>
</html>