📜  打印反金字塔字符模式的程序

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

打印反金字塔字符模式的程序

给定一个正整数 n,将逆金字塔模式打印到 n 行,如示例中所示。
例子 :

Input : 4
Output :

A B C D D C B A
  A B C C B A
    A B B A
      A A
 
Input : 6
Output :

 A B C D E F F E D C B A
   A B C D E E D C B A
     A B C D D C B A
       A B C C B A
         A B B A
           A A

下面是该模式的实现:

C++
// C++ code to print inverse
// pyramid pattern
#include 
using namespace std;
 
// function to print the
// inverse pyramid pattern
void pyramid(int n)
{
    int i, j, num, gap;
 
    // outer loop to handle number
    // of rows n in this case
    for (i = n; i >= 1; i--) {
         
        // inner loop to create right triangle
        // gaps on left side of pyramid
        for (gap = n - 1; gap >= i; gap--) {
            cout<<" ";
            cout<<" ";
        }
         
        // initializing value corresponding
        // to 'A' ASCII value
        num = 'A';
 
        // loop to print characters on
        // left side of pyramid
        for (j = 1; j <= i; j++) {
            cout << (char) num++ <<" ";
        }
         
        // loop to print characters on
        // right side of pyramid
        for (j = i - 1; j >= 0; j--) {
            cout << (char) --num <<" ";
        }
        cout<<"\n";
    }
}
 
// Driver function
int main()
{
    int n = 9;
    pyramid(n);
    return 0;
}


Java
// Java code for Inverse Pyramid
import java.util.*;
 
class GFG {
     
    // function for inverse pyramid print
    static void pyramid(int n)
    {
        int i, j, num, gap;
     
        // outer loop to handle number
        // of rows n in this case
        for (i = n; i >= 1; i--) {
             
            // inner loop to create right triangle
            // gaps on left side of pyramid
            for (gap = n - 1; gap >= i; gap--) {
                System.out.print(" ");
                System.out.print(" ");
            }
             
            // initializing value corresponding
            // to ASCII value of 'A'
            num = 'A';
     
            // loop to print characters on
            // left side of pyramid
            for (j = 1; j <= i; j++) {
                System.out.print((char)num++ + " ");
            }
             
            // loop to print characters on
            // right side of pyramid
            for (j = i - 1; j >= 0; j--) {
                System.out.print((char)--num + " ");
            }
             
            System.out.println("");
        }
    }
     
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n = 9;
        pyramid(n);
     
    }
}
 
// This article is contributed by Gitanjali.


Python3
# Python3 code to print inverse
# pyramid pattern
 
# function to print the following
# inverse pyramid pattern
def pyramid( n ):
     
    # outer loop to handle number
    # of rows n in this case
    for i in range(n, 0, -1):
         
        # inner loop to create right triangle
        # gaps on left side of pyramid
        for gap in range(n-1, i-1, -1):
            print(" ", end = '')
            print(" ", end = '')
         
        # initializing value corresponding
        # to 'A' ASCII value
        num = ord('A')
         
        # loop to print characters on
        # left side of pyramid
        for j in range(1, i+1):
            print(chr(num), end = ' ')
            num += 1
             
        # loop to print characters on
        # right side of pyramid
        for j in range(i - 1, -1, -1):
            num -= 1
            print(chr(num), end = ' ')
         
        print("\n", end = '')
 
# Driver Code
n = 9
pyramid(n)
 
# This code is contributed by "Sharad_Bhardwaj".


C#
//C# code for Inverse Pyramid
using System;
 
class GFG {
     
    // function for inverse pyramid print
    static void pyramid(int n)
    {
        int i, j, num, gap;
     
        // outer loop to handle number
        // of rows n in this case
        for (i = n; i >= 1; i--) {
             
            // inner loop to create right triangle
            // gaps on left side of pyramid
            for (gap = n - 1; gap >= i; gap--) {
                Console.Write(" ");
                Console.Write(" ");
            }
             
            // initializing value corresponding
            // to ASCII value of 'A'
            num = 'A';
     
            // loop to print characters on
            // left side of pyramid
            for (j = 1; j <= i; j++) {
                Console.Write((char)num++ + " ");
            }
             
            // loop to print characters on
            // right side of pyramid
            for (j = i - 1; j >= 0; j--) {
              Console.Write((char)--num + " ");
            }
             
            Console.WriteLine("");
        }
    }
     
    /* Driver program to test above function */
    public static void Main()
    {
        int n = 9;
        pyramid(n);
     
    }
}
 
// This article is contributed by vt_m.


PHP
= 1; $i--)
    {
         
        // inner loop to create
        // right triangle gaps on
        // left side of pyramid
        for ($gap = $n - 1; $gap >= $i;
                            $gap--)
        {
            echo"  ";
        }
         
        // initializing value corresponding
        // to 'A' ASCII value is 65
        $num = 65;
 
        // loop to print characters on
        // left side of pyramid
        for ($j = 1; $j <= $i; $j++)
        {
            echo chr($num++)." ";
        }
         
        // loop to print characters on
        // right side of pyramid
        for ($j = $i - 1; $j >= 0; $j--)
        {
            echo chr(--$num)." ";
        }
        echo"\n";
    }
}
 
// Driver Code
$n = 9;
pyramid($n);
 
// This code is contributed by mits
?>


Javascript


输出 :

A B C D E F G H I I H G F E D C B A
   A B C D E F G H H G F E D C B A
     A B C D E F G G F E D C B A
       A B C D E F F E D C B A
         A B C D E E D C B A
           A B C D D C B A
             A B C C B A
               A B B A
                 A A