이 기사는 C++ 프로그래밍을 사용하여 흥미로운 패턴을 인쇄합니다. 알고리즘은 다음과 같습니다.
알고리즘
Step-1 Define the size which will be double automatically Step-2 Print the upper section using a loop Step-3 Print the lower section using a loop
예시
위의 알고리즘을 기반으로 다음과 같은 C++ 코드가 새겨져 있습니다.
#include <iostream>
using namespace std;
int main(){
int n=3;
int i,j;
// This is upper half of pattern
for (i=1; i<=n; i++){
for (j=1; j<=(2*n); j++){
// Left part of pattern
if (i<j)
cout<<" ";
else
cout<<"*";
// Right part of pattern
if (i<=((2*n)-j))
cout<<" ";
else
cout<<"*";
}
cout<<endl;
}
// This is lower half of pattern
for (i=1; i<=n; i++){
for (j=1;j<=(2*n);j++){
// Left part of pattern
if (i>(n-j+1))
cout<<" ";
else
cout<<"*";
// Right part of pattern
if ((i+n)>j)
cout<<" ";
else
cout<<"*";
}
cout<<endl;
}
return 0;
} 위의 코드를 컴파일하면 다음과 같은 흥미로운 패턴이 인쇄됩니다.
출력
* * * * * * * * * * * * * * * * * * * * * * * *