[PHP Example] Several methods to implement the multiplication table (three loops, four angles, and one recursion)

 Watson Blog December 14, 2017 00:15:26 PHP technology comment nine hundred and eighty-one Reading mode

Implementing the multiplication table in PHP is a practical problem encountered in the interview. Today, I will summarize it. It's nothing for a veteran. It's easy. But for a novice like me, writing in different ways is quite a training of logical thinking.

  1. <? php   
  2.   
  3. //1、 Use the for loop to print the 99 multiplication table:   
  4. for ( $j =1;   $j <=9;   $j ++) {  
  5. for ( $i =1;   $i <= $j ;   $i ++) {  
  6. echo   "{$i}x{$j}=" .( $i * $j ). " " ;   
  7. }  
  8. echo   "<br />" ;   
  9. }  
  10.   
  11. //2、 Use the while loop to print the nine nine multiplication table   
  12.   
  13. $j  = 1;   
  14. while ( $j <=9){  
  15. $i  = 1;   
  16. while ( $i <= $j ){  
  17. echo   "{$i}x{$j}=" .( $i * $j ). " " ;   
  18. $i ++;   
  19. }  
  20. echo   "<br />" ;   
  21. $j ++;   
  22. }  
  23.   
  24.   
  25.   
  26. //3、 Use do while loop to print 99 multiplication table   
  27. $j  = 1;   
  28. do  {  
  29. $i  = 1;   
  30. do  {  
  31. echo   "{$i}x{$j}=" .( $i * $j ). " " ;   
  32. $i ++;   
  33. while ( $i <= $j );   
  34. echo   "<br />" ;   
  35. $j ++;   
  36. while ( $j <=9);   
  37.   
  38. //Now use the for loop to output the multiplication table in table form   
  39. //Angle 1: (the most common conventional writing method)   
  40.   
  41. echo   "<table border='1'>" ;   
  42. for ( $j =1; $j <=9; $j ++){  
  43. echo   "<tr>" ;   
  44. for ( $i =1; $i <= $j ; $i ++){  
  45. echo   "<td>{$i}*{$j}=" .( $i * $j ). "</td>" ;   
  46. }  
  47. echo   "</tr>" ;   
  48. }  
  49. echo   "</table>" ;   
  50.   
  51. //Angle 2: (X axis symmetry with conventional writing)   
  52.   
  53.   
  54. echo   "<table border='1'>" ;   
  55. for ( $j =9; $j >=1; $j --){  
  56. echo   "<tr>" ;   
  57. for ( $i =1; $i <= $j ; $i ++){  
  58. echo   "<td>{$i}*{$j}=" .( $i * $j ). "</td>" ;   
  59. }  
  60. echo   "</tr>" ;   
  61. }  
  62. echo   "</table>" ;   
  63.   
  64.   
  65. //Angle 3: (symmetrical to the Y axis of angle 2)   
  66.   
  67. echo   "<table border='1'>" ;   
  68. for ( $j =9; $j >=1; $j --){  
  69. echo   "<tr>" ;   
  70. for ( $z =0; $z <9- $j ; $z ++){  
  71. echo   "<td> </td>" ;   
  72. }  
  73. for ( $i =1; $i <= $j ; $i ++){  
  74. echo   "<td>{$i}*{$j}=" .( $i * $j ). "</td>" ;   
  75. }  
  76. echo   "</tr>" ;   
  77. }  
  78. echo   "</table>" ;   
  79.   
  80. //Angle 4: (Y axis symmetry with conventional writing)   
  81.   
  82. echo   "<table border='1'>" ;   
  83. for ( $j =1; $j <=9; $j ++){  
  84. echo   "<tr>" ;   
  85. for ( $z =0; $z <9- $j ; $z ++){  
  86. echo   "<td> </td>" ;   
  87. }  
  88. for ( $i = $j ; $i >=1; $i --){  
  89. echo   "<td>{$i}*{$j}=" .( $i * $j ). "</td>" ;   
  90. }  
  91. echo   "</tr>" ;   
  92. }  
  93. echo   "</table>" ;   
  94.   
  95.   
  96.   
  97. //Recursive writing   
  98.   
  99. //@ param int: $i number   
  100. //@ param int: $k layers   
  101.   
  102. function  rideNum( $i =1,  $k =1){  
  103.      if ( $i  >= 10 ||  $k  >= 10){ exit ;}   
  104.      echo   $i .'x'. $k .'='. $i * $k .'  ';   
  105.    
  106. #The number of outer layers is the same as that of the number, and the next page is broken
  107.      if ( $i  ==  $k ){   
  108.          echo  '<br/>';   
  109.         rideNum(1, $k +1);   
  110.     }  
  111. #If the number is not enough, continue+1, and the Bank will continue to output
  112.     rideNum( $i +1,  $k );   
  113. }  
  114. rideNum(1,1);   

 

 Watson Blog
  • This article is written by Published on December 14, 2017 00:15:26
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/873.html

Comment