输入n 高度= 2 * N + 1;
if n=1
*
**
*
if n=2
*
* *
* * * * *
* *
*
if n=3
*
* *
* * *
* * * * * * * * *
* * *
* *
*
依旧......
是两条垂直恒星之间的'n'星的间隙。
答案 0 :(得分:1)
美味的foreach:
<?php
function draw_stars($n)
{
foreach(range(1, $n) as $i) {
foreach(range(1, $i) as $k)
$pattern[] = '*';
$pattern[] = str_repeat('*', 2 * $i + 1);
}
foreach($pattern as $k => $stars)
$pattern[$k] = str_split(
str_pad($stars, 2 * $n + 1, ' ', STR_PAD_BOTH)
);
$pattern = transpose($pattern);
foreach($pattern as $line)
echo implode('', $line), "\n";
}
function transpose($array) {
return array_map(null, ...$array);
}