自学内容网 自学内容网

C# Program to print pyramid pattern (打印金字塔图案的程序)

 编写程序打印由星星组成的金字塔图案 

例子 : 

输入:n = 6
输出:
       *
       * *
       * * *
       * * * *
       * * * * *
       * * * * * * 
       * * * * *
       * * * *
       * * *
       * * 
       *
 


我们强烈建议您最小化浏览器并先自己尝试一下。
这个想法是对金字塔的每个部分使用两个 for 循环。这两个部分可以分为上部和下部 

示例代码:

// C# program to print Pyramid pattern
using System;
 
class GFG {
    public static void pattern(int n)
    {
        // For printing the upper
        // part of the pyramid
        for (int i = 1; i < n; i++) {
            for (int j = 1; j < i + 1; j++) {
                Console.Write(" * ");
            }
            Console.WriteLine();
        }
 
        // For printing the lower
        // part of pyramid
        for (int i = n; i > 0; i--) {
            for (int j = i; j > 0; j--) {
                Console.Write(" * ");
            }
            Console.WriteLine();
        }
    }
 
    // Driver program
    public static void Main()
    {
        pattern(6);
    }
}
 
// This code is contributed by vt_m.

输出 : 
 *
 *  *
 *  *  *
 *  *  *  *
 *  *  *  *  *
 *  *  *  *  *  *
 *  *  *  *  *
 *  *  *  *
 *  *  *
 *  *
 *

时间复杂度: O(n 2 )

辅助空间: O(1)


原文地址:https://blog.csdn.net/hefeng_aspnet/article/details/140131830

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!