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)

相关推荐
jiao000011 小时前
数据结构——队列
c语言·数据结构·算法
迷迭所归处2 小时前
C++ —— 关于vector
开发语言·c++·算法
leon6253 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
CV工程师小林3 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
Navigator_Z3 小时前
数据结构C //线性表(链表)ADT结构及相关函数
c语言·数据结构·算法·链表
Aic山鱼3 小时前
【如何高效学习数据结构:构建编程的坚实基石】
数据结构·学习·算法
天玑y4 小时前
算法设计与分析(背包问题
c++·经验分享·笔记·学习·算法·leetcode·蓝桥杯
sjsjs114 小时前
【数据结构-一维差分】力扣1893. 检查是否区域内所有整数都被覆盖
数据结构·算法·leetcode
redcocal4 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘
码了三年又三年4 小时前
【算法】滑动窗口—找所有字母异位词
算法