用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