Example, Yang Hui triangle code implemented in PHP.
-
-
error_reporting(0); - // $iLine=5;
//Function to output Yang Hui triangle
- function YangHui( $iLine)
- {
- for ($i = 0;$i <= $iLine;$i++)//line
- {
- 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 two values of the previous row are the same Add
- echo $a[$i][$j]." ";
- }
- }
- }
- // return $a;
- }
//Call example, print Yang Hui triangle
- YangHui(5);
- ?>
-
Copy code
|