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

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

例子 : 

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


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

示例代码:

// C++ program to print Pyramid pattern
#include<iostream>
using namespace std;
 
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++){
            cout <<" * ";
        }
        cout << endl ;
    }
 
    // For printing the lower part of pyramid
    for (int i = n; i > 0; i--){
        for (int j = i; j > 0; j--){
            cout <<  " * ";
        }
        cout << endl ;
    }
}
 
// Driver program 
int main()
{
    pattern(6);
    return 0;
}

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

时间复杂度: O(n 2 )

辅助空间: O(1)

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值