Programa para imprimir un Triángulo Hueco dentro de un Triángulo

Dado un número N (≥ 8), la tarea es imprimir un triángulo hueco dentro de un patrón de triángulo.
Ejemplo: 
 

Input: N = 9
Output:
                *                 
              *   *               
            *       *             
          *     *     *           
        *     * * *     *         
      *                   *       
    *                       *     
  *                           *   
* * * * * * * * * * * * * * * * * 

Acercarse: 
 

Sea i el índice de las filas y j el índice de las columnas. Después: 
 

  • Para los lados del triángulo exterior: 
    si el índice de la columna ( j ) es igual a ( N – i + 1 ) o ( N + i – 1 ), entonces se imprime ‘*’ para los lados iguales del triángulo exterior. 
     
if(j == (N - i + 1)
   || j == (N + i - 1) {
  print('*')
}
  •  
  • Para los lados del triángulo interior: 
    si el (índice de la fila ( i ) es menor que ( N – 4 ) y mayor que ( 4 ) y el índice de la columna ( j ) es igual a ( N – i + 4 ) o ( N + i + 4 ), luego se imprime ‘*’ para los lados iguales del triángulo interior. 
     
if(  (i >= 4 
     && i <= n - 4) 
  && (j == N - i + 4 
     || j == N + i - 4) ) {
    print('*')
}
  •  
  • Para las bases del triángulo exterior: 
    si el índice de la fila ( i ) es igual a N , entonces se imprime ‘*’ para la base del triángulo exterior. 
     
if(i == N) {
   print('*')
}
  •  
  • Para las bases del triángulo interior: 
    si el índice de fila ( i ) es igual a (N – 4) y el índice de columna ( j ) debe ser mayor que igual a ( N – (N – 2*4) ), y j es menor que es igual a ( N + N – 2*4 ), luego se imprime ‘*’ para la base del triángulo interior. 
     
if( (i == N - 4) 
 && (j >= N - (N - 2 * 4) ) 
 && (j <= n + n - 2 * 4) ) ) {
   print('*')
}
  •  

A continuación se muestra la implementación del enfoque anterior: 
 

CPP

// C++ implementation of the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the pattern
void printPattern(int n)
{
 
    int i, j;
 
    // Loop for rows
    for (i = 1; i <= n; i++) {
 
        // Loop for column
        for (j = 1; j < 2 * n; j++) {
 
            // For printing equal sides
            // of outer triangle
            if (j == (n - i + 1)
                || j == (n + i - 1)) {
                cout << "* ";
            }
 
            // For printing equal sides
            // of inner triangle
            else if ((i >= 4 && i <= n - 4)
                     && (j == n - i + 4
                         || j == n + i - 4)) {
 
                cout << "* ";
            }
 
            // For printing base
            // of both triangle
            else if (i == n
                     || (i == n - 4
                         && j >= n - (n - 2 * 4)
                         && j <= n + n - 2 * 4)) {
 
                cout << "* ";
            }
 
            // For spacing between the triangle
            else {
                cout << " "
                     << " ";
            }
        }
        cout << "\n";
    }
}
 
// Driver Code
int main()
{
    int N = 9;
 
    printPattern(N);
}

Java

// Java implementation of the above approach
import java.util.*;
 
class GFG{
  
// Function to print the pattern
static void printPattern(int n)
{
  
    int i, j;
  
    // Loop for rows
    for (i = 1; i <= n; i++) {
  
        // Loop for column
        for (j = 1; j < 2 * n; j++) {
  
            // For printing equal sides
            // of outer triangle
            if (j == (n - i + 1)
                || j == (n + i - 1)) {
                System.out.print("* ");
            }
  
            // For printing equal sides
            // of inner triangle
            else if ((i >= 4 && i <= n - 4)
                     && (j == n - i + 4
                         || j == n + i - 4)) {
  
                System.out.print("* ");
            }
  
            // For printing base
            // of both triangle
            else if (i == n
                     || (i == n - 4
                         && j >= n - (n - 2 * 4)
                         && j <= n + n - 2 * 4)) {
  
                System.out.print("* ");
            }
  
            // For spacing between the triangle
            else {
                System.out.print(" "
                    + " ");
            }
        }
        System.out.print("\n");
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 9;
  
    printPattern(N);
}
}
 
// This code is contributed by sapnasingh4991

Python3

# Python3 implementation of the above approach
 
# Function to print the pattern
def printPattern(n):
 
    # Loop for rows
    for i in range(1, n + 1):
 
        # Loop for column
        for j in range(1, 2 * n):
 
            # For printing equal sides
            # of outer triangle
            if (j == (n - i + 1)
                or j == (n + i - 1)):
                print("* ",end="")
 
            # For printing equal sides
            # of inner triangle
            elif ((i >= 4 and i <= n - 4)
                    and (j == n - i + 4
                        or j == n + i - 4)):
 
                print("* ",end="")
 
            # For printing base
            # of both triangle
            elif (i == n
                    or (i == n - 4
                        and j >= n - (n - 2 * 4)
                        and j <= n + n - 2 * 4)):
 
                print("* ", end="")
 
            # For spacing between the triangle
            else :
                print(" "+" ", end="")
 
        print()
 
# Driver Code
N = 9
 
printPattern(N)
 
# This code is contributed by mohit kumar 29

C#

// C# implementation of the above approach
using System;
 
class GFG{
   
// Function to print the pattern
static void printPattern(int n)
{
   
    int i, j;
   
    // Loop for rows
    for (i = 1; i <= n; i++) {
   
        // Loop for column
        for (j = 1; j < 2 * n; j++) {
   
            // For printing equal sides
            // of outer triangle
            if (j == (n - i + 1)
                || j == (n + i - 1)) {
                Console.Write("* ");
            }
   
            // For printing equal sides
            // of inner triangle
            else if ((i >= 4 && i <= n - 4)
                     && (j == n - i + 4
                         || j == n + i - 4)) {
   
                Console.Write("* ");
            }
   
            // For printing base
            // of both triangle
            else if (i == n
                     || (i == n - 4
                         && j >= n - (n - 2 * 4)
                         && j <= n + n - 2 * 4)) {
   
                Console.Write("* ");
            }
   
            // For spacing between the triangle
            else {
                Console.Write(" "
                    + " ");
            }
        }
        Console.Write("\n");
    }
}
   
// Driver Code
public static void Main(String[] args)
{
    int N = 9;
   
    printPattern(N);
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
 
      // JavaScript implementation of the above approach
 
      // Function to print the pattern
      function printPattern(n) {
        var i, j;
 
        // Loop for rows
        for (i = 1; i <= n; i++) {
          // Loop for column
          for (j = 1; j < 2 * n; j++) {
            // For printing equal sides
            // of outer triangle
            if (j == n - i + 1 || j == n + i - 1) {
              document.write("* ");
            }
 
            // For printing equal sides
            // of inner triangle
            else if (
              i >= 4 &&
              i <= n - 4 &&
              (j == n - i + 4 || j == n + i - 4)
            ) {
              document.write("* ");
            }
 
            // For printing base
            // of both triangle
            else if (
              i == n ||
              (i == n - 4 && j >= n - (n - 2 * 4) && j <= n + n - 2 * 4)
            ) {
              document.write("* ");
            }
 
            // For spacing between the triangle
            else {
              document.write(" ");
              document.write("  ");
            }
          }
          document.write("<br>");
        }
      }
 
      // Driver Code
 
      var N = 9;
 
      printPattern(N);
       
    </script>
Producción: 

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

 

Complejidad temporal: O(n 2 )

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por ujjwal_ab 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 *