杨辉三角(php版)

11 篇文章 0 订阅
<!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>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值