在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;
}编译完上面的代码后,有趣的图案将如下所示。
输出结果
* * * * * * * * * * * * * * * * * * * * * * * *
热门推荐
10 订婚祝福语简短诗句
11 钢琴获奖老师祝福语简短
12 开丁大吉祝福语 简短
13 公司生日祝福语员工简短
14 团队虎年祝福语简短霸气
15 正月十五简短谜语祝福语
16 给老公祝福语简短2026
17 公司年会祝福语简短最好
18 送同学的简短祝福语