: This article describes how to use PHP to output symmetric Diamond. For more information about PHP tutorials, see. For example, to output a simple diamond or Star Tower, the first is to control the row number on the outermost layer, and then the cycle on the inner layer. the cycle on the inner layer has two first cycle controls, then, the second loop controls the number of stars. the space is the total number of rows minus the current number of rows for ($ space = 1; $ space <= $ lines-$ line; + + $ space) then the cycle of the stars is for ($ star = 1; $ star <= 2 * $ line-1; ++ $ star)
The following code is part of the simplest output Diamond.
// $ Line indicates the row number // $ space = $ lines-$ line; // leading space of each line // $ star = 2 * $ line-1; // m the number of stars in each row // The number of stars in the outer loop control line for ($ line = 1; $ line <= $ lines; ++ $ line) {// output leading spaces first, control the number of spaces for ($ space = 1; $ space <= $ lines-$ line; ++ $ space) {echo '+';} // output the stars, control the number of stars for ($ star = 1; $ star <= 2 * $ line-1; ++ $ star) {echo '*';} // output line feed echo"
";}
Symmetric graphs are mostly involved in the number axis. for example, the sporadic output shown in the figure is replaced by spaces.
First, the general method is to first create a + + loop to output the upper half layer, and then a -- loop to output the lower half layer.
For example
$lines=12;for ($line=1; $line <=$lines ; $line++) { for ($j=1; $j <= $lines - $line ; $j++) { echo "+";}for ($i=0; $i < 2*$line-1; $i++) { if ($i == 0 || $i == 2*$line-2) {echo "*";}else{echo "+";};}echo "
";}$lines=12;for ($line=$lines-1; $line >=1 ; $line--) { for ($j=1; $j <= $lines - $line ; $j++) { echo "+";}for ($i=0; $i < 2*$line-1; $i++) { if ($i == 0 || $i == 2*$line-2 || $line == $lines) {echo "*";}else{echo "+";};}echo "
";}Then there is a clever way to use the number axis
// 123454321
//-4-3-2-101234 (number axis)
// 432101234 (absolute value)
Think of the effect of 1 2 3 4 5 4 3 2 1
First, convert-4-3-2-101234 using the absolute value function.
And then 432101234
If the first sum is equal to the same book, for example, this is equal to 5.
Corresponding pseudo code
for($i=-4; $i<=4; ++$i) {echo $i, '+';echo abs($i), '+';echo 5-abs($i);echo '
';}
According to this idea, the first loop of the outermost layer controls the number axis, that is, to go from a negative number to a positive number for ($ I =-($ lines-1 ); $ I <= $ lines-1; ++ $ I)
Then the total number of rows minus the absolute value of the preceding number axis $ line = $ lines-abs ($ I );
Then the following code will remain unchanged.
For ($ I =-($ lines-1); $ I <= $ lines-1; ++ $ I) {// calculate $ line = $ lines-abs ($ I); // output leading space first to control the number of spaces for ($ space = 1; $ space <= $ lines-$ line; ++ $ space) {echo '+';} // output the stars and control the number of stars. for ($ star = 1; $ star <= 2 * $ line-1; ++ $ star) {// determine whether to output stars or spaces. if ($ star = 1 | $ star = 2 * $ line-1) {echo '*';} else {echo '+'; }}// output line feed echo"
";}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above describes how to use PHP to output symmetric Diamond, including some content. I hope my friends who are interested in PHP tutorials can help me.