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

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

时间:2024-07-24 10:59:18浏览次数:10  
标签:Diamond Console space C# void Write int Shape row

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

例子 :

// C# Code to print 
// the diamond shape
using System;

class GFG 
{
    
    // Prints diamond pattern
    // with 2n rows
    static 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++)
                Console.Write(" ");
    
            // Print i+1 stars
            for (int j = 0; j <= i; j++)
                Console.Write("* ");
    
            Console.Write("\n");
            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++)
                Console.Write(" ");
    
            // Print i stars
            for (int j = 0; j < i; j++)
                Console.Write("* ");
    
            Console.Write("\n");
            space++;
        }
    }
    
    // Driver Code
    public static void Main() 
    {
        printDiamond(5);
        
    }
}

// This code is contributed 
// by Smitha Semwal. 

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

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

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

下面是上述方法的实现:

using System;

public class GFG{

  public static void gotonextLine(int k, int i, int z)
  {
    if (k == i) // base case
      return;
    Console.Write("* ");
    gotonextLine(k + z, i, z);
  }

  public static void addblankSpaceInDiamond(
    int j, int i, int z) // print blank space of diamond
  {
    if (j == i)
      return;
    Console.Write(" ");
    addblankSpaceInDiamond(j + z, i, z);
  }

  public static void upperDiamond(int row, int i)
  {
    if (i > row) // base case
      return;
    addblankSpaceInDiamond(row, i, -1);
    gotonextLine(0, i, 1);
    Console.Write("\n");
    upperDiamond(row, i + 1); // recursive call
  }


  public static 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);
    Console.Write("\n");
    lowerDiamond(row, i + 1);
  }

  public static void Main ()
  {

    // Code
    int row;
    row = 5;
    upperDiamond(row, 0); // print upper part of triangle
    lowerDiamond(row, 1); // print lower part of diamond
  }
}

// This code is contributed by akashish__

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

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

标签:Diamond,Console,space,C#,void,Write,int,Shape,row
From: https://blog.csdn.net/hefeng_aspnet/article/details/140601293

相关文章

  • JAVA 打印菱形的程序(Program to print the Diamond Shape)
    给定一个数字n,编写一个程序来打印一个有2n行的菱形。例子://JAVACodetoprint //thediamondshapeimportjava.util.*;classGFG{     //Printsdiamondpattern  //with2nrows  staticvoidprintDiamond(intn)  {    i......
  • C++ 打印菱形的程序(Program to print the Diamond Shape)
    给定一个数字n,编写一个程序来打印一个有2n行的菱形。例子:  //C++programtoprintdiamondshape//with2nrows #include<bits/stdc++.h>usingnamespacestd;//Printsdiamondpatternwith2nrows voidprintDiamond(intn) {   intspace=n......
  • 【RawSocket】RawSocket是什么?
    1.RawSocket简介RawSocket是数据链路层的socketRawsocket(原始套接字)是一种特殊的网络套接字类型,它允许应用程序直接发送和接收底层的网络数据包,而不需要经过标准的传输层协议(如TCP或UDP)的处理。以下是rawsocket的一些关键特点:直接访问网络层:Rawsocket允许应用程序直接......
  • 【简单易懂,复制可运行】C++通讯录管理系统实现增删改查
    自己写的300行c++通讯录管理系统,可以实现如下功能: 具体代码如下:#include<iostream>usingnamespacestd;#defineMax1000//不要分号//设计联系人结构体structPerson{ stringm_Name; intm_Sex; intm_Age; stringm_Phone; stringm_Addr; };//设计......
  • Kubernetes Secret 详解
    KubernetesSecret是一种用于存储和管理敏感信息的对象,如密码、OAuth令牌和SSH密钥等。使用Secret可以避免将机密数据直接放在Pod规约或容器镜像中,从而增加了应用程序的安全性。Secret的类型Kubernetes支持多种类型的Secret,包括:​​Opaque​​:默认的Secret类......
  • Warning formula.jws.json update failed, falling back to cached version
    Warning:formula.jws.json:updatefailed,fallingbacktocachedversion.MacOS执行服务启动命令时,显示下载失败警告,导致无法启动服务MacBook-Pro~%brewservicesstartnginx==>Downloadinghttps://formulae.brew.sh/api/formula.jws.json########......
  • ASTGNN(Localised Adaptive Spatial-Temporal Graph Neural Network)
    引言        本文主要探讨的问题是:能否以及在多大程度上对时空图模型进行局部化。并且将研究领域集中到ASTGNN上。ASTGNNs通常使用自适应图卷积层对空间依赖性进行建模。通过学习图邻接矩阵来捕获空间依赖性。因此,ASTGNN的局部化是通过邻接矩阵(仅捕获空间依赖性)的稀疏......
  • PHP中static的使用
    本文由ChatMoney团队出品PHP,作为一种面向对象的编程语言,为开发者提供了丰富的特性和功能,助力构建高效的应用程序。其中,static关键字在类和方法中的应用尤为重要,它提供了一种独特的变量和方法访问机制。本文将深入探讨static关键字的使用方法,并通过具体代码示例来指导实践。一......
  • Codeforces Round 961 (Div. 2)
    A#include<bits/stdc++.h>usingnamespacestd;inta[200];voidsolve(){intn,k;cin>>n>>k;a[1]=n;for(intj=n-1,i=2;i<=1+(n-1)*2;i+=2,j--){a[i]=a[i+1]=j;}if(!k){cout<<0<<"\n......
  • Android 13 大屏显示时关于SystemUI和Launcher3问题
    当系统运行在大屏上时,原来显示SystemUI导航栏的位置会变成Launcher3的任务栏,然后导航栏的3个按键显示靠右下角显示1.先看SystemUI的导航栏为什么会消失,移动/SystemUI/src/com/android/systemui/statusbar/NavigationBarController.javapublicvoidcreateNavigationBar......