C++風箏圖案列印程式
在本文件中,我們將討論一個用於列印給定風箏圖案的程式。
為此,我們將輸入設定為 N=5。我們的任務是列印給定風箏結構,其總高度為 2N+1 = 5。其中包括用於上部菱形結構的 9 行以及用於下部不完整菱形結構的 2 行。
示例
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
int main(){
int i, j, k, sp, space = 4;
char prt = '$';
//printing the upper half of the first diamond
for (i = 1; i <= 5; i++){
//printing the spaces in the front
for (sp = space; sp >= 1; sp--){
cout << " ";
}
//printing $ character
for (j = 1; j <= i; j++){
cout << prt;
}
for (k = 1; k <= (i - 1); k++){
if (i == 1){
continue;
}
cout << prt;
}
cout << "\n";
space--;
}
space = 1;
//printing the lower half of the first diamond
for (i = 4; i >= 1; i--){
for (sp = space; sp >= 1; sp--) {
cout << " ";
}
for (j = 1; j <= i; j++){
cout << prt;
}
for (k = 1; k <= (i - 1); k++){
cout << prt;
}
space++;
cout << "\n";
}
space = 3;
//printing the second incomplete diamond
for (i = 2; i <= 5; i++){
if ((i % 2) != 0){
for (sp = space; sp >= 1; sp--){
cout << " ";
}
for (j = 1; j <= i; j++){
cout << prt;
}
}
if ((i % 2) != 0) {
cout << "\n";
space--;
}
}
return 0;
}輸出
$ $$$ $$$$$ $$$$$$$ $$$$$$$$$ $$$$$$$ $$$$$ $$$ $ $$$ $$$$$
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP