C++ language output output diamond

Output the following graphics:

             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
divides the above graphics into two parts, the
first part is the upper triangle (including the middle part), and the
second part is the lower triangle.
The ideas above and below are the same.
For the upper triangle, the space is reduced by one each time, and the output graph is increased by two each time.
The code corresponding space_init--; star_init+=2;
upper triangular There are five rows, so with a click on the While loop output.
The figure below is the opposite of the above, just change the initial value.

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

Guess you like

Origin blog.csdn.net/weixin_44895666/article/details/108570115