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

私のアイデアは、
2つの部分に上記グラフィックを分割し、
最初の部分は、(中央部分を含む)上三角であり、そして
第二の部分は低い三角形です。
上と下の考え方は同じです。
上三角の場合、スペースは毎回1つ減少し、出力グラフは毎回2つ増加します。上三角に
対応するコードspace_init--; star_init+=2;
5つの行があるため、Whileループ出力をクリックします。
下の図は上記の逆で、初期値を変更するだけです。

C ++ソースコード

#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;
 } 

おすすめ

転載: blog.csdn.net/weixin_44895666/article/details/108570115