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)

相关推荐
r_oo_ki_e_22 分钟前
牛牛与切割机
java·算法
小糖学代码27 分钟前
LLM系列:4.transformer算法:4.NLTK(补充)
深度学习·算法·transformer
geovindu3 小时前
python: PCA
开发语言·后端·python·算法
凯程序猿1号4 小时前
基于射线法与球面外积的WGS84电子围栏点在多边形内判定及边界容差算法
算法·电脑
budingxiaomoli4 小时前
动态规划---子序列
算法·动态规划
释厄6234 小时前
尺子必回量与劣币生良币——可证伪明的尺子性·同源可转化
大数据·算法·开源
天诰4 小时前
SA-T-06 轨迹数据工程 脏数据检测
人工智能·python·算法·语言模型·ai编程
Warson_L6 小时前
.NET 8 速成笔记 16:API 框架到底怎么用,谁是自带的、谁是自己写的、配置和测试库如何关联
c#·.net
Shaoshing6 小时前
LeetCode 921.使括号括号有效的最少添加
java·算法·leetcode·职场和发展
xurileidian6 小时前
第 30 篇 · 边玩边学Python · 猜数字小游戏 把判断循环异常串起来
前端·python·算法