📜  打印逆菱形图案的程序

📅  最后修改于: 2022-05-13 01:57:59.302000             🧑  作者: Mango

打印逆菱形图案的程序

给定一个整数 n,任务是打印 2n-1 行的逆菱形图案。
例子:

Input: n = 3
Output:

*** ***
**   **
*     *
**   **
*** ***

Input: n = 7
Output:

******* *******
******   ******
*****     *****
****       ****
***         ***
**           **
*             *
**           **
***         ***
****       ****
*****     *****
******   ******
******* *******

方法:

  1. 对于 n 的输入,完整的 Inverse Diamond 有 2n-1 行。
  2. 该程序分为两部分打印此图案:
    • 第一部分是 n 行菱形的上半部分。
    • 这部分包括3个部分——*的左三角形、空间的中间三角形和*的右三角形。
    • 第二部分是 n-1 行菱形的下半部分。
    • 这部分还包括3个部分——*的左三角形、空间的中间三角形和*的右三角形。
  3. 打印每个部分,获得所需的反向菱形图案。

下面是上述方法的实现:

C++
// C++ Code to print
// the inverse diamond shape
   
#include
 using namespace std;
   
    // Function to Print Inverse Diamond pattern
    // with 2n-1 rows
void printDiamond(int n)
    {
        cout<Java
// Java Code to print
// the inverse diamond shape
 
import java.util.*;
 
class GFG {
 
    // Function to Print Inverse Diamond pattern
    // with 2n-1 rows
    static void printDiamond(int n)
    {
        System.out.println();
 
        int i, j = 0;
 
        // for top half
        for (i = 0; i < n; i++) {
 
            // for left *
            for (j = i; j < n; j++)
                System.out.print("*");
 
            // for middle " "
            for (j = 0; j < 2 * i + 1; j++)
                System.out.print(" ");
 
            // for right *
            for (j = i; j < n; j++)
                System.out.print("*");
 
            System.out.println();
        }
 
        // for below half
        for (i = 0; i < n - 1; i++) {
 
            // for left *
            for (j = 0; j < i + 2; j++)
                System.out.print("*");
 
            // for middle " "
            for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
                System.out.print(" ");
 
            // for right *
            for (j = 0; j < i + 2; j++)
                System.out.print("*");
 
            System.out.println();
        }
 
        System.out.println();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        // Define n
        int n = 3;
        System.out.println("Inverse Diamond Pattern for n = " + n);
        printDiamond(n);
 
        n = 7;
        System.out.println("\nInverse Diamond Pattern for n = " + n);
        printDiamond(n);
    }
}


Python3
#Python3 program to print
# the inverse diamond shape
     
 
# Function to Print Inverse
# Diamond pattern
# with 2n-1 rows
def printDiamond(n) :
    print("")
    j = 0
     
    # for top half
    for i in range(0,n):
         
        # for left *
        for j in range(i,n):
            print("*",end="")
             
        # for middle " "
        for j in range(0,2 * i + 1):
            print(" ",end="")
             
        # for right *
        for j in range(i,n):
            print("*",end="")
             
        print("")
     
    # for below half
    for i in range(0,n-1):
         
        # for left *
        for j in range(0, i+2):
            print("*",end="")
             
        # for middle " "
        for j in range(0,2 * (n - 1 - i) - 1):
            print(" ",end="")
             
        # for right *
        for j in range(0, i+2):
            print("*",end="")
             
        print("")
         
    print("")
     
     
# Driver Code
 
if __name__=='__main__':
     
# Define n
    n = 3
    print("Inverse Diamond Pattern for n = ",n)
    printDiamond(n)
     
    n = 7
    print("\nInverse Diamond Pattern for n = ",n )
    printDiamond(n)
 
# this code is contributed by Smitha Dinesh Semwal


C#
// C# Code to print
// the inverse diamond shape
using System;
 
class GFG
{
 
// Function to Print Inverse
// Diamond pattern with 2n-1 rows
static void printDiamond(int n)
{
    Console.WriteLine();
 
    int i, j = 0;
 
    // for top half
    for (i = 0; i < n; i++)
    {
 
        // for left *
        for (j = i; j < n; j++)
            Console.Write("*");
 
        // for middle " "
        for (j = 0; j < 2 * i + 1; j++)
            Console.Write(" ");
 
        // for right *
        for (j = i; j < n; j++)
            Console.Write("*");
 
        Console.WriteLine();
    }
 
    // for below half
    for (i = 0; i < n - 1; i++)
    {
 
        // for left *
        for (j = 0; j < i + 2; j++)
            Console.Write("*");
 
        // for middle " "
        for (j = 0; j < 2 * (n - 1 - i) - 1; j++)
            Console.Write(" ");
 
        // for right *
        for (j = 0; j < i + 2; j++)
            Console.Write("*");
 
        Console.WriteLine();
    }
 
    Console.WriteLine();
}
 
// Driver Code
public static void Main()
{
 
    // Define n
    int n = 3;
    Console.WriteLine("Inverse Diamond " +
                      "Pattern for n = " + n);
    printDiamond(n);
 
    n = 7;
    Console.WriteLine("\nInverse Diamond " +
                        "Pattern for n = " + n);
    printDiamond(n);
}
}
 
// This code is contributed
// by inder_verma.


PHP


Javascript


输出:
Inverse Diamond Pattern for n = 3

*** ***
**   **
*     *
**   **
*** ***


Inverse Diamond Pattern for n = 7

******* *******
******   ******
*****     *****
****       ****
***         ***
**           **
*             *
**           **
***         ***
****       ****
*****     *****
******   ******
******* *******