C++语言输出输出菱形

输出下面图形:

             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;
 } 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值