首页 > 编程语言 >C++ 打印菱形的程序(Program to print the Diamond Shape)

C++ 打印菱形的程序(Program to print the Diamond Shape)

时间:2024-07-24 10:58:49浏览次数:10  
标签:Diamond return cout space int C++ Shape print row

给定一个数字n ,编写一个程序来打印一个有2n行的菱形。

例子 :

  

// C++ program to print diamond shape
// with 2n rows 
#include <bits/stdc++.h>
using namespace std;

// Prints diamond pattern with 2n rows 
void printDiamond(int n) 

    int space = n - 1; 

    // run loop (parent loop) 
    // till number of rows 
    for (int i = 0; i < n; i++) 
    { 
        // loop for initially space, 
        // before star printing 
        for (int j = 0;j < space; j++) 
            cout << " "; 

        // Print i+1 stars 
        for (int j = 0; j <= i; j++) 
            cout << "* "; 

        cout << endl; 
        space--; 
    } 

    // Repeat again in reverse order 
    space = 0; 

    // run loop (parent loop) 
    // till number of rows 
    for (int i = n; i > 0; i--) 
    { 
        // loop for initially space, 
        // before star printing 
        for (int j = 0; j < space; j++) 
            cout << " "; 

        // Print i stars 
        for (int j = 0;j < i;j++) 
            cout << "* ";

        cout << endl;
        space++; 
    } 

// Driver code 
int main() 

    printDiamond(5); 
    return 0; 

// This is code is contributed
// by rathbhupendra

输出
    *
   * *
  * * *
 * * * *
* * * * *
* * * * *
 * * * *
  * * *
   * *
    *

时间复杂度: O(n*n),因为我们正在遍历网格的行和列来打印空格 ' '和星号 '*'。
辅助空间: O(1),不使用额外空间。 

方法 2:使用递归解决问题

下面是上述方法的实现:

// C++ program to print diamond pattern using recursion
#include <bits/stdc++.h>
using namespace std;

void gotonextLine(int k, int i, int z)
{
    if (k == i) // base case
        return;
    cout << "* ";
    gotonextLine(k + z, i, z);
}

void addblankSpaceInDiamond(
    int j, int i, int z) // print blank space of diamond
{
    if (j == i)
        return;
    cout << " ";
    addblankSpaceInDiamond(j + z, i, z);
}

void upperDiamond(int row, int i)
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    cout << endl;
    upperDiamond(row, i + 1); // recursive call
}

void lowerDiamond(int row,
                  int i) // print the next line of diamond
{
    if (i > row) // base case
        return;
    addblankSpaceInDiamond(0, i, 1);
    gotonextLine(row, i, -1);
    cout << endl;
    lowerDiamond(row, i + 1);
}

int main()
{
    int row;
    row = 5;
    upperDiamond(row, 0); // print upper part of triangle
    lowerDiamond(row, 1); // print lower part of diamond
    return 0;
    // this code is contributed by Shivesh Kumar Dwivedi
}

输出
     
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *
    

时间复杂度: O(N 2 ),因为我们要遍历网格的行和列来打印空格 ' ' 和星号 '*'。
辅助空间: O(N),额外的空间用于递归调用堆栈。

标签:Diamond,return,cout,space,int,C++,Shape,print,row
From: https://blog.csdn.net/hefeng_aspnet/article/details/140600581

相关文章

  • 【简单易懂,复制可运行】C++通讯录管理系统实现增删改查
    自己写的300行c++通讯录管理系统,可以实现如下功能: 具体代码如下:#include<iostream>usingnamespacestd;#defineMax1000//不要分号//设计联系人结构体structPerson{ stringm_Name; intm_Sex; intm_Age; stringm_Phone; stringm_Addr; };//设计......
  • 【C++】string类(上)
    个人主页~string一、标准库中的string类1、什么是string类2、string类的常用接口讲解(1)string类的常见构造(2)string类的容量操作(3)string类对象的访问及遍历(4)string类对象的修改(5)string类非成员函数(6)其他(7)vs和g++下string结构说明vs下的string结构g++下string结构......
  • c++_爆刷n题
    P1089[NOIP2004提高组]津津的储蓄计划题目描述津津的零花钱一直都是自己管理。每个月的月初妈妈给津津300300元钱,津津会预算这个月的花销,并且总能做到实际花销和预算的相同。为了让津津学习如何储蓄,妈妈提出,津津可以随时把整百的钱存在她那里,到了年末她会加上20%20%......
  • C++ 运算符重载的注意事项
    C++的运算符重载是一种强大的特性,它允许开发者为已存在的运算符赋予新的含义,以适应特定数据类型的需求。在使用运算符重载时,需要注意以下几点:不改变运算符原有语义:运算符重载应保持运算符原有的基本含义,避免引起混淆。例如,重载加法运算符+时,应确保其结果与常规加法操作相符。......
  • C++进阶 继承
    目录继承的概念及定义继承概念继承定义定义格式 继承关系和访问限定符 继承基类成员访问方式的变化基类和派生类对象赋值转换继承中的作用域派生类的默认成员函数构造函数 拷贝构造函数 赋值运算符重载析构函数总结继承与友元继承与静态成员浅谈复杂的菱......
  • 通讯录管理系统(C++基础知识实现)
    通讯录管理系统描述:本人C++小白一枚,正在学习C++基础知识,给大家分享一款使用C++基础知识实现的通讯录管理系统,一起努力进步,大佬轻点喷。1.知识点(1)预处理器指令(#include,#define);(2)命名空间使用(usingnamespacestd;);(3)函数定义:定义了多个函数,如menu,addContact,show......
  • 【C++】模版初阶
    模版一.泛型编程二.函数模版1.函数模版的概念2.函数模板的格式3.函数模版的原理4.函数模版的实例化5.模板参数的匹配原则三.类模版1.类模板的定义格式2.类模板的实例化一.泛型编程当我们要交换两个变量时,可以使用函数重载,如下:voidSwap(int&x,int&y){}voidS......
  • C++之迭代器
    1.什么是迭代器?2.如何使用迭代器3.C++迭代器说明4.迭代器的高级应用4.1.Enumerator.hpp4.2.Iterator.cpp4.3.输出结果4.4.更多详细代码1.什么是迭代器?迭代器(Iterator)是按照一定的顺序对一个或多个容器中的元素从前往遍历的一种机制,比如for循环就是一种最......
  • C++学习之路——第二天(指针和引用)
    指针和二维数组概述:不管是几维数组,当你使用数组名a而没有取地址操作符&时,它(a)都会退化为指向其第一层的指针。(一维数组指向首元素的指针,二维数组是第一行数组的指针,三维数组是指向其第一层(也就是第一个二维数组)的指针)1、错误示范inta[3][4]={{1,2,3,4},{5,6,7,8},{9......
  • C++_模板(初阶)
    C++_模板(初阶)泛型编程如何实现一个通用的交换函数呢?voidSwap(int&left,int&right){inttemp=left;left=right;right=temp;}voidSwap(double&left,double&right){doubletemp=left;left=right;right=temp;}v......