用C++列印有趣的模式
這篇文章使用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;
}在上文中編譯程式碼後,有趣的模式將如下所示列印。
產出
* * * * * * * * * * * * * * * * * * * * * * * *
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP