이 튜토리얼에서는 주어진 역 다이아몬드 패턴을 인쇄하는 프로그램에 대해 논의할 것입니다.
이를 위해 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;
} 출력
***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** *****