PHP prints a solid and hollow diamond with side length N

WBOY
Release: 2016-08-08 09:29:20
Original
1191 people have browsed it

php prints a solid and hollow rhombus with side length N

Solid rhombus calculation method:

$n: side length
$i: current line, starting from 0
$rows: Total number of rows

Upper
Number of preceding spaces =$n-$i-1
Number of characters =$i*2+1
Lower
Number of preceding spaces =$i-$n+1
Number of characters =($rows-$i)*2-1

Using str_pad can reduce for/while loops

/**
 * 打印实心菱型
 * @param  int    $n 边长,默认5
 * @param  String $s 显示的字符, 默认*
 * @return String
 */
function solidDiamond($n=5, $s='*'){

    $str = '';

    // 计算总行数
    $rows = $n*2-1;

    // 循环计算每行的*
    for($i=0; $i<$rows; $i++){
        if($i<$n){ // 上部
            $str .= str_pad(&#39;&#39;, ($n-$i-1), &#39; &#39;). str_pad(&#39;&#39;, $i*2+1, $s)."\r\n";
        }else{     // 下部
            $str .= str_pad(&#39;&#39;, ($i-$n+1), &#39; &#39;). str_pad(&#39;&#39;, ($rows-$i)*2-1, $s). "\r\n";
        }
    }

    return $str;

}

echo &#39;<xmp>';
echo solidDiamond(5);
echo '</xmp>';
Copy after login

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
Copy after login

Hollow diamond calculation method:
$n: side length
$i: current row, starting from 0
$rows: total number of rows
upper part
number of preceding spaces=$n-$i-1
number of empty spaces=$i*2+1-2
number of characters=$i* 2+1 - Number of empty spaces
lower part
number of front spaces=$i-$n+1
number of empty spaces=($rows-$i)*2-1-2
number of characters=($rows-$ i)*2-1 - Number of empty spaces

/**
 * 打印空心菱型
 * @param  int    $n 边长,默认5
 * @param  String $s 显示的字符, 默认*
 * @return String
 */
function hollowDiamond($n=5, $s='*'){

    $str = '';

    // 计算总行数
    $rows = $n*2-1;

    // 循环计算每行的*
    for($i=0; $i<$rows; $i++){
        if($i<$n){ // 上部
            $tmp = $i*2+1;
            $str .= str_pad(&#39;&#39;, ($n-$i-1), &#39; &#39;). str_pad(str_pad(&#39;&#39;, $tmp-2, &#39; &#39;, STR_PAD_BOTH), $tmp, $s, STR_PAD_BOTH)."\r\n";
        }else{     // 下部
            $tmp = ($rows-$i)*2-1;
            $str .= str_pad(&#39;&#39;, ($i-$n+1), &#39; &#39;). str_pad(str_pad(&#39;&#39;, $tmp-2, &#39; &#39;, STR_PAD_BOTH), $tmp, $s, STR_PAD_BOTH). "\r\n";
        }
    }

    return $str;

}

echo &#39;<xmp>';
echo hollowDiamond(5);
echo '</xmp>';
Copy after login

    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
Copy after login

The above introduces PHP to print a solid and hollow diamond shape with side length N, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
str
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template