给定一个数字n ,编写一个程序来打印一个有2n行的菱形。
例子 :

<?php
// PHP program to print
// diamond shape with
// 2n rows
// Prints diamond $
// pattern with 2n rows
function printDiamond($n)
{
$space = $n - 1;
// run loop (parent loop)
// till number of rows
for ($i = 0; $i < $n; $i++)
{
// loop for initially space,
// before star printing
for ($j = 0;$j < $space; $j++)
printf(" ");
// Print i+1 stars
for ($j = 0;$j <= $i; $j++)
printf("* ");
printf("\n");
$space--;
}
// Repeat again in
// reverse order
$space = 0;
// run loop (parent loop)
// till number of rows
for ($i = $n; $i > 0; $i--)
{
// loop for initially space,
// before star printing
for ($j = 0; $j < $space; $j++)
printf(" ");
// Pr$i stars
for ($j = 0;$j < $i;$j++)
printf("* ");
printf("\n");
$space++;
}
}
// Driver code
printDiamond(5);
// This code is contributed by Anuj_67
?>
输出
*
* *
* * *
* * * *
* * * * *
* * * * *
* * * *
* * *
* *
*

时间复杂度: O(n*n),因为我们正在遍历网格的行和列来打印空格 ' '和星号 '*'。
辅助空间: O(1),不使用额外空间。
491

被折叠的 条评论
为什么被折叠?



