C++程序 打印钻石形状
给定一个数 n ,写一个程序来打印有 2n-1 行的钻石形状。
例子:
输入:5
输出:
// C++ program to print diamond shape
// with 2n-1 rows
#include <bits/stdc++.h>
using namespace std;
// Prints diamond pattern with 2n-1 rows
void printDiamond(int n)
{
int space = n - 1;
// run loop (parent loop)
// till number of rows
for (int i = 0; i < n; i++)
{
// loop for initially space,
// before star printing
for (int j = 0;j < space; j++)
cout << " ";
// Print i+1 stars
for (int j = 0; j <= i; j++)
cout << "* ";
cout << endl;
space--;
}
// Repeat again in reverse order
space = 0;
// run loop (parent loop)
// till number of rows
for (int i = n-1; i > 0; i--)
{
// loop for initially space,
// before star printing
for (int j = 0; j < space; j++)
cout << " ";
// Print i stars
for (int j = 0;j < i;j++)
cout << " *";
cout << endl;
space++;
}
}
// Driver code
int main()
{
printDiamond(5);
return 0;
}
// This is code is contributed
// by rathbhupendra```
输出
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
时间复杂度: O(n*n)
,因为我们正在遍历网格的行和列来打印空格 “ ” 和星号 “*” 。
辅助空间: O(1),没有使用额外的空间。