How to print a solid and Hollow Diamond with side length N by php
This article mainly shows you "php how to print a side length of N solid and hollow diamond", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "php how to print a side length of N solid and hollow diamond" this article.
This paper describes the method of printing a solid and hollow diamond with a side length of N by php. Share it with you for your reference. The specific analysis is as follows:
Solid diamond calculation method:
$n: side length
$I: current line, starting with 0
$rows: total number of lines
Upper part
Number of leading spaces = $n-$i-1
Number of characters = $iTunes 2 characters 1
Lower part
Number of leading spaces = $i-$n+1
Number of characters = ($rows-$i) * 2-1
Using str_pad can reduce cycles such as for/while
/ * * print solid diamond * @ param int $n side length. Default is 5 * @ param String $s. Default is * * @ return String * / function solidDiamond ($str =''). / / calculate the total number of lines $rows = $nasty 2-1; / / cycle the * for ($iS0; $I) of each line