Making pyramid using c#

后端 未结 13 2068
花落未央
花落未央 2021-01-03 11:04

My question is how to make a pyramid using * and \'space\' in C#? The output will be like this.

     *
    * *
   * * *
  * * * *
 * * * * *
<
13条回答
  •  别那么骄傲
    2021-01-03 11:25

    think about how you'd print the pyramid manually.

    suppose 5 levels deep.

    1st line: 4 spaces, 1 star,
    2nd line: 3 spaces, star, space, star
    3rd line: 2 spaces, star space star space star
    

    etc.

    doesn't matter whether you print spaces after the last star or not - won't make a difference to how it looks.

    what do we see?

    if we have a total of X levels

    line 1: (x-1) spaces, (star space)
    line 2: (x-2) spaces, (star space) twice
    line 3: (x-3) spaces, (star space) three times
    line 4: (x-4) spaces, (star space) four times
    

    that's the pattern. I'll leave the coding to you.

提交回复
热议问题