Home >Backend Development >PHP Tutorial >PHP implementation of Yang Hui triangle example

PHP implementation of Yang Hui triangle example

WBOY
WBOYOriginal
2016-07-25 09:12:582297browse

Example, Yang Hui triangle code implemented in PHP.

  1. error_reporting(0);

  2. // $iLine=5;

  3. //Function to output Yang Hui triangle

  4. function YangHui( $iLine)
  5. {
  6. for ($i = 0;$i <= $iLine;$i++)//line
  7. {
  8. for ($j = 0;$j <= $i;$j++)// Column
  9. {
  10. if ($i == $j)//row = column (that is, the last column) or the first row and the first column
  11. {
  12. $a[$i][$j] = 1;
  13. echo $a[$i][$j]."
    ";
  14. }
  15. else if ($i != 0 && $j == 0)//row=column (that is, the last column) or the first Row and first column
  16. {
  17. $a[$i][$j] = 1;
  18. echo $a[$i][$j]." ";
  19. }
  20. else
  21. {
  22. $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
  23. echo $a[$i][$j]." ";
  24. }
  25. }
  26. }
  27. // return $a;
  28. }

  29. //Call example, print Yang Hui triangle

  30. YangHui(5);
  31. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn