Example, PHP code to implement Yang Hui's triangle.
-
-
error_reporting(0); - // $iLine=5;
//Output Yang Hui triangle
- function YangHui($iLine )
- {
- for ($i = 0;$i <= $iLine;$i++)//row
- {
- for ($j = 0;$j <= $i;$j++)//column
- {
- if ($i == $j)//row = column (that is, the last column) or the first row and the first column
- {
- $a[$i][$j] = 1;
- echo $a [$i][$j]."
";
- }
- else if ($i != 0 && $j == 0)//row=column (that is, the last column) or the first row and First column
- {
- $a[$i][$j] = 1;
- echo $a[$i][$j]." ";
- }
-
- else
- {
- $a[$i ][$j] = $a[$i-1][$j]+$a[$i-1][$j-1];//The value of the row + column = the sum of the 2 values of the previous row
- echo $a[$i][$j]." ";
- }
- }
- }
- // return $a;
- }
-
- YangHui(5);
- ?>
-
Copy code
|