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;
} 出力
***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** *****
-
Cでダイヤモンドパターンを印刷するプログラム
プログラムの説明 ダイアモンドパターンは、単純なピラミッドパターンと逆ピラミッドパターンを組み合わせたものです。 アルゴリズム First Row: Display 1 Second Row: Display 1,2,3 Third Row: Display 1,2,3,4,5 Fourth Row: Display 1,2,3,4,5,6,7 Fifth Row: Display 1,2,3,4,5,6,7,8,9 Display the same contents from 4th Row till First Row below the fifth Row. 例 /* Program
-
中空のピラミッドとダイヤモンドのパターンをC++で印刷するプログラム
ここでは、C++を使用して中空のピラミッドとダイヤモンドのパターンを生成する方法を説明します。ソリッドピラミッドパターンを非常に簡単に生成できます。中空にするには、いくつかのトリックを追加する必要があります。 中空ピラミッド 最初の行のピラミッドには1つの星が印刷され、最後の行にはn個の星が印刷されます。他の行の場合、行の開始と終了に正確に2つの星が印刷され、これら2つの開始の間に空白があります。 サンプルコード #include <iostream> using namespace std; int main() { int n, i, j; &nbs