输出下面图形:
0
0 0 0
0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0
0 0 0
0
My idea
将上述图形分成两部分,
第一部分为上面的三角形(包括中间部分),
第二部分为下面的三角形。
上面下面实现的思路是一致的。
对于上面三角形,空格每次减少一个,输出图形每次增加两个。
分别对应代码 space_init--; star_init+=2;
上面的三角形总共有5行,所以用While循环输出一下就可以。
下面的图形与上面是相反的,更改一下初始化的值即可。
C++ source code
#include <iostream>
int main()
{
using namespace std;
int row_total = 5; // initialize the num of total line;
int space_init = 4; // the first line of the space is 4;
int star_init = 1; // the fisrt line of the space is 1;
int space; // variable of space_init;
int star; // variable of star_init;
// output the upper one
while(row_total > 0){
space = space_init;
star = star_init;
while(space > 0){
cout << " ";
space--;
} space_init--;
while(star > 0){
cout << " 0 ";
star--;
} star_init+=2;
row_total--;
cout << endl; // Wrap
}
// output the lower one
row_total = 4;
space_init = 1;
star_init = 7;
//#if 0 // Use this statement to compile
while(row_total > 0){
space = space_init;
star = star_init;
while(space > 0){
cout << " ";
space --;
} space_init++;
while(star > 0){
cout << " 0 ";
star --;
} star_init -= 2;
row_total--;
cout << endl; // Wrap
}
//#endif // Use this statement to compile
return 0;
}