PHP print Yang Hui triangle integrated Example

Source: Internet
Author: User

Example, php print Yang Hui triangle.

  1. $params =getparams (1);

  2. $ARGV 0=trim ($params [0]);
  3. if (!is_numeric ($argv 0))
  4. {
  5. Error_msg ("Params 1 must be a numbers");
  6. }

  7. $spaceNumber = 6;

  8. $MAXN = $argv 0;
  9. Output ("", true);
  10. Get_trangle ($argv 0);
  11. Error_msg ("execute success");
  12. function Get_trangle ($n) {
  13. if ($n <= 0)
  14. {//bbs.it-home.org
  15. return false;
  16. }
  17. if ($n ==1)
  18. {
  19. $this _level=array (1);
  20. Print_line ($this _level, $n);
  21. return Array (1);
  22. }
  23. if ($n ==2)
  24. {
  25. $this _level=array (a);
  26. Print_line (Array (1), 1);
  27. Print_line ($this _level, $n);
  28. return $this _level;
  29. }
  30. $last _level=get_trangle ($n-1);
  31. if (!is_array ($last _level) | | Count ($last _level) < 2)
  32. {
  33. return false;
  34. }
  35. $this _level=array ();
  36. $this _level[0]=1;
  37. for ($i =0; $i < count ($last _level)-1; $i + +)
  38. {
  39. $this _level[$i +1]= $last _level[$i]+ $last _level[$i +1];
  40. }
  41. $this _level[]=1;
  42. Print_line ($this _level, $n);
  43. return $this _level;
  44. }
  45. function Print_line ($aArray, $n)
  46. {
  47. Global $MAXN, $spaceNumber;
  48. $line =sprintf ("%". ($MAXN-$n) * $spaceNumber/2). " S "," ");
  49. foreach ($aArray as $i)
  50. {
  51. $line. =sprintf ("%". $spaceNumber. " S ", $i);
  52. }
  53. Output ($line);
  54. }
  55. function Getparams ($paramNum)
  56. {
  57. $in =file_get_contents ("In.txt");
  58. if ($in ===false) {
  59. Error_msg ("Cannot read in.txt,please check in.txt exists\n");
  60. }
  61. $in =preg_replace ("/(\s+)/I", "", $in);
  62. $parms =split ("", Trim ($in));
  63. if ($parms ===false)
  64. {
  65. Error_msg ("Cannot get param from in.txt\n");
  66. }
  67. if (count ($parms) < $paramNum)
  68. {
  69. Error_msg ("It needs $paramNum params\n");
  70. }
  71. return $parms;
  72. }
  73. Output Yang Hui triangle
  74. function output ($msg, $isClean =false)
  75. {
  76. if ($isClean)
  77. {
  78. $handle = fopen (' OUT.txt ', ' W ');
  79. Fclose ($handle);
  80. }
  81. Error_log ($msg. " \ n ", 3," OUT.txt ");
  82. }
  83. function error_msg ($msg, $is _exit=true)
  84. {
  85. if ($is _exit)
  86. Die ($msg. " \ n ");
  87. Else
  88. echo $msg. " \ n ";
  89. }
  90. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.