Programa para imprimir un patrón de caracteres de pirámide inversa

Dado un entero positivo n, imprima el patrón de pirámide inversa hasta n filas como se muestra en los ejemplos.
Ejemplos: 
 

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

A continuación se muestra la implementación del patrón: 
 

C++

// C++ code to print inverse
// pyramid pattern
#include <bits/stdc++.h>
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

<?php
// PHP implementation to print
// inverse pyramid pattern
 
// function to print the
// inverse pyramid pattern
function pyramid($n)
{
    // 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--)
        {
            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

<script>
 
      // JavaScript code to print inverse
      // pyramid pattern
 
      // function to print the
      // inverse pyramid pattern
      function pyramid(n)
      {
        var 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--) {
            document.write("    ");
          }
 
          // initializing value corresponding
          // to 'A' ASCII value
          num = "A".charCodeAt(0);
 
          // loop to print characters on
          // left side of pyramid
          for (j = 1; j <= i; j++) {
            document.write(String.fromCharCode(num++)
            + "  ");
          }
 
          // loop to print characters on
          // right side of pyramid
          for (j = i - 1; j >= 0; j--) {
            document.write(String.fromCharCode(--num)
            + "  ");
          }
          document.write("<br>");
        }
      }
 
      // Driver function
      var n = 9;
      pyramid(n);
       
</script>
Producción

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 

Complejidad de tiempo: O(n 2 ), donde n representa la entrada dada.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Publicación traducida automáticamente

Artículo escrito por shreyanshi_arun y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *