杨辉三角(php版)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title>网页标题</title>
	<meta name="keywords" content="关键字列表" />
	<meta name="description" content="网页描述" />
	<link rel="stylesheet" type="text/css" href="" />
	<style type="text/css"></style>
	<script type="text/javascript"></script>
</head>
<body>

	<form action ="day8_yanghui.php" method ="post">
		
		<input type="text" name ="txt" />

		<input type ="submit"  value ="输出" />
	</form>
<?php

//获得总行数
$math = isset($_POST['txt'])?$_POST['txt']:0;

//控制行号
for($i =1;$i<=$math;++$i)
{
	//控制前导空格
	for($n=1; $n<=$math-$i; ++$n)
	{
		echo " ";
	}
	
	//控制 每行上字符的数量(字符包括数字 与 数字间的空格)
	for($j=1;$j<=2*$i-1;++$j)
	{

		if($j==1 || $j==(2*$i-1))
		{
			$arr[$i][$j] = 1;
			echo $arr[$i][$j];
		}
		else if($j % 2 ==0)
		{
			$arr[$i][$j] = 0;
		}
		else
		{
			$arr[$i][$j]  = $arr[($i-1)][$j-2] + $arr[($i-1)][($j)];
			echo $arr[$i][$j];
		}
		if($j % 2 ==0)
		{
			echo " ";
		}

	}
	echo "<br >";
}

//	echo "<pre>";

	//var_dump($arr);

?>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-cn">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title>网页标题</title>
	<meta name="keywords" content="关键字列表" />
	<meta name="description" content="网页描述" />
	<link rel="stylesheet" type="text/css" href="" />
	<style type="text/css"></style>
	<script type="text/javascript"></script>
</head>
<body>
<?php
//function yanghui($lines) {
//	for($i=1; $i<=$lines; ++$i) {
//		for($j=1; $j<=$i; ++$j) {
//			//第一列,对角线
//			for($n=1;$n<=($lines-$i)*5;++$n)
//			{
//				echo " ";
//			}
//			if($j==1 || $j==$i) {
//				echo $data[$i][$j] = 1," "," "," "," ";//先赋值,再输出
//			} else {
//				echo $data[$i][$j] = $data[$i-1][$j] + $data[$i-1][$j-1]," "," "," "," ";
//			}
//		}
//	}
//}
//yanghui(10);

function yanghui($lines) {
	echo "<table>";
	for($i=1; $i<=$lines; ++$i) {
		echo "<tr>";
		for($n=1;$n<=$lines-$i;++$n)
			{
				echo "<td>"," ","</td>";
			}
		
		for($j=1; $j<=$i; ++$j) {

			//第一列,对角线
			
			if($j==1 || $j==$i) {
				echo "<td>";
				echo $data[$i][$j] = 1;//先赋值,再输出
				echo "</td>";
			} else {
				echo "<td>";
				echo $data[$i][$j] = $data[$i-1][$j] + $data[$i-1][$j-1];
				echo "</td>";
			}
			
			echo "<td>"," ","</td>";
		}
		echo "</tr>";
		echo "<br />";
	}
	echo "</table>";
}
yanghui(10);
?>
</body>
</html>
    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/pkgray/article/details/31144125
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞