ZCMU-5118

There is a wave of construction problems (the captain is absent, water for a day)


topic

5118: Tree!

Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 23 Solved: 12
[Submit][Status][Web Board]
Description

Little M is a graphics lover. Christmas is coming soon. He wants to draw a nice Christmas tree with a computer as a Christmas gift for his girlfriend (he actually has no girlfriend, he is just dreaming), but his IQ Limited, it is impossible to draw a perfect Christmas tree, so he needs your help.

A tree is composed of multiple triangles and a rectangular trunk. Enter a number n, which represents the number of layers of the tree.

The first layer of a tree consists of 3 rows, the second layer has 4 rows, and so on.

At the end there will be n layers of tree trunks.

For a more specific look, you can observe the sample.

Input

The first line of input contains a number T, which means there are T test samples.

Each test contains only one number n, which is the number of layers of a Christmas tree. (n >= 1)

Output

For each test, just draw the corresponding tree.

After each tree is printed, please make a blank line at the end. See the sample for details.

Sample Input

2
1
2
Sample Output

  *
 * *
*****
 ***
   *
  * *
 *****
   *
  * *
 *   *
*******
  * *
  ***

Algorithm ideas

This question looks cumbersome, but in fact, we only need to solve these problems
1. The structure of the root
2. The structure of the tree.
We can find that n is the number of spaces before the root, and it is also the number of layers of the root; add one The special judgment is that the roots of the last layer (bottom) are filled with three, and the rest are hollow in the middle, so the CreatRoot module can be written very simply

As for the CreatTree module, it is a little more complicated. We noticed that the space tree before the beginning of each tree is fixed, that is, 2+(n-1) spaces. When building the tree, except for the first and last lines that are not hollow, the rest are in accordance with 1 3 5··· · The regular arrangement of 2*(k-1)-1.

AC code

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
const int maxn = 1e5+5;
typedef long long ll;

void CreatTree(int n){
    
    
    int s = 2+(n-1);//初始化开头空格数
    //造n个树
    for(int i=1;i<=n;i++){
    
    
       
        int sum = 2 + i;//第i个树的层数
        //造第i个树
        int temp = s;
        for(int k=1;k<=sum;k++){
    
    
        for(int j=0;j<temp;j++)printf(" ");
            if(k==sum)for(int m=0;m<sum*2-1;m++)printf("*");
            else{
    
    
                printf("*");
                //造中间的空格
                for(int m=0;m<((k-1)*2-1);m++)printf(" ");
                //第一个只有一个*
                if(k!=1)
                printf("*");
            }
            temp--;
            printf("\n");
        }
        
    }
}
void CrearRoot(int n){
    
    
    for(int i=0;i<n;i++){
    
    
        for(int j=0;j<n;j++)printf(" ");
        if(i!=n-1)printf("* *");
        else printf("***");
        printf("\n");
    }
}
int main()
{
    
    
    
    int T,n;
    scanf("%d",&T);
    while(T--){
    
    
        scanf("%d",&n);
        CreatTree(n);
        CrearRoot(n);
        //注意,需要换行
        cout << endl;
    }

    return 0;
}


to sum up

Simple structure, water problem

Guess you like

Origin blog.csdn.net/DAVID3A/article/details/114678980