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)

相关推荐
王哈哈^_^20 分钟前
【完整源码+数据集】草莓数据集,yolov8草莓成熟度检测数据集 3207 张,草莓成熟度数据集,目标检测草莓识别算法系统实战教程
人工智能·算法·yolo·目标检测·计算机视觉·视觉检测·毕业设计
baivfhpwxf202342 分钟前
要在 WPF 中实现数据表对应实体的属性与 UI 控件的双向绑定,并支持修改通知和 UI 自动更新
c#·wpf
油泼辣子多加1 小时前
【实战】自然语言处理--长文本分类(3)HAN算法
算法·自然语言处理·分类
Shinom1ya_1 小时前
算法 day 46
数据结构·算法
秋月的私语1 小时前
代码自动生成文本小工具TextStringizerWpf
c#
葛小白11 小时前
Winform控件:Chart
c#·winform·chart
夏鹏今天学习了吗2 小时前
【LeetCode热题100(64/100)】搜索旋转排序数组
算法·leetcode·职场和发展
2301_796512522 小时前
Rust编程学习 - 问号运算符会return一个Result 类型,但是如何使用main函数中使用问号运算符
开发语言·学习·算法·rust
小龙报2 小时前
算法通关指南:数据结构和算法篇 --- 队列相关算法题》--- 1. 【模板】队列,2. 机器翻译
c语言·开发语言·数据结构·c++·算法·学习方法·visual studio
晨非辰3 小时前
【数据结构初阶】--从排序算法原理分析到代码实现操作,参透插入排序的奥秘!
c语言·开发语言·数据结构·c++·算法·面试·排序算法