在 C++ 中打印反向钻石图案的程序
在本教程中,我们将讨论一个程序以打印给定的反向钻石图案。
为此,我们将获得 N 值。我们的任务是根据 2N-1 的高度打印反向钻石图案。
示例
#include<bits/stdc++.h>
using namespace std;
//printing the inverse diamond pattern
void printDiamond(int n){
cout<<endl;
int i, j = 0;
//loop for the upper half
for (i = 0; i < n; i++) {
//left triangle
for (j = i; j < n; j++)
cout<<"*";
//middle triangle
for (j = 0; j < 2 * i + 1; j++)
cout<<" ";
//right triangle
for (j = i; j < n; j++)
cout<<"*";
cout<<endl;
}
//loop for the lower half
for (i = 0; i < n - 1; i++) {
//left triangle
for (j = 0; j < i + 2; j++)
cout<<"*";
//middle triangle
for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
cout<<" ";
//right triangle
for (j = 0; j < i + 2; j++)
cout<<"*";
cout<<endl;
}
cout<<endl;
}
int main(){
int n = 5;
printDiamond(n);
return 0;
}输出
***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** *****
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP