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)

相关推荐
NorburyL8 分钟前
DPO笔记
深度学习·算法
老纪的技术唠嗑局19 分钟前
深度解析 LLM Wiki / Obsidian-Wiki / GBrain:Agent 时代知识的“自组织”与“自进化”
大数据·数据库·人工智能·算法
hixiong1232 小时前
C# TensorRT部署RF-DETR目标检测&分割模型
人工智能·目标检测·计算机视觉·ai·c#
YXXY3133 小时前
模拟算法的介绍
算法
happymaker06264 小时前
简单LRU的实现(基于LinkedHashMap)
算法·leetcode·lru
会编程的土豆4 小时前
【数据结构与算法】空间复杂度从入门到面试:不仅会算,还要会解释
数据结构·c++·算法·面试·职场和发展
普通网友4 小时前
《算法面试必刷:15 个高频 LeetCode 题(附代码)》
算法·leetcode·面试
_深海凉_4 小时前
LeetCode热题100-搜索二维矩阵
算法·leetcode·矩阵
张槊哲4 小时前
C++ 进阶指南:如何丝滑地理解与实践多线程与多进程
开发语言·c++·算法
代码中介商5 小时前
C语言链表完全指南:从单节点到链表管理
c语言·算法·链表