Programa para imprimir patrones de estrellas diagonales

Complejidad de tiempo: O(N) donde N es el número de Nodes en un árbol binario dado

Espacio auxiliar: O(N)Para la entrada dada, este programa imprime el siguiente patrón. La entrada debe ser un número impar.
Ejemplos: 
 

Input : 7
Output :

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

A continuación se muestra el código que se imprime arriba del patrón: 
 

C++

// CPP program to print diagonal star patterns
#include <iostream>
using namespace std;
 
void pattern(int n)
{
    // Loop denoting rows
    for (int i = 0; i < n; i++) {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking boundary conditions and main
            // diagonal and secondary diagonal conditions
            if (i == 0 || j == 0 || i == j || i == n - 1
                            || j == n - 1 || i + j == n - 1)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
    // n denotes size which should be odd
    int n = 7;
    // Function calling
    pattern(n);
    return 0;
}

Java

// Java program to print diagonal star patterns
 
import java.util.*;
import java.lang.*;
 
public class GfG{
    public static void pattern(int n)
    {
    // Loop denoting rows
    for (int i = 0; i < n; i++) {
             
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking boundary conditions
            // and main diagonal and
            // secondary diagonal conditions
            if (i == 0 || j == 0 || i == j
                || i == n - 1 || j == n - 1
                || i + j == n - 1)
                    System.out.print("*");
            else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
     
    // Driver function
    public static void main(String argc[]){
 
        // n denotes size which should be odd
        int n = 7;
 
        // Function calling
        pattern(n);
    }
     
}
 
// This code is contributed by Sagar Shukla

Python3

# Python 3 program to print
# diagonal star patterns
 
def pattern(n) :
     
    # Loop denoting rows
    for i in range(0 , n) :
 
        # Loop denoting columns
        for j in range(0 , n) :
             
            # Checking boundary conditions and main
            # diagonal and secondary diagonal conditions
            if (i == 0 or j == 0 or i == j 
               or i == n - 1 or j == n - 1
               or i + j == n - 1) :
                print( "*", end="")
            else :
                print(" ",end="")
         
        print("")
     
     
# Driver code
# n denotes size which should be odd
n = 7
 
# Function calling
pattern(n)
 
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to print diagonal
// star patterns
using System;
 
public class GfG{
    public static void pattern(int n)
    {
    // Loop denoting rows
    for (int i = 0; i < n; i++) {
             
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking boundary conditions,
            // main diagonal and secondary
            // diagonal conditions
            if (i == 0 || j == 0 || i == j
                || i == n - 1 || j == n - 1
                || i + j == n - 1)
                    Console.Write("*");
            else
                    Console.Write(" ");
            }
            Console.WriteLine();
        }
    }
     
    // Driver function
    public static void Main(){
 
        // n denotes size which should be odd
        int n = 7;
 
        // Function calling
        pattern(n);
    }
     
}
 
// This code is contributed by vt_m.

PHP

<?php
// php program to print
// diagonal star patterns
 
function pattern($n)
{
     
    // Loop denoting rows
    for ($i = 0; $i < $n; $i++)
    {
         
        // Loop denoting columns
        for ($j = 0; $j < $n; $j++)
        {
             
            // Checking boundary conditions
            // and main diagonal and secondary
            // diagonal conditions
            if ($i == 0 || $j == 0 || $i == $j ||
                $i == $n - 1 || $j == $n - 1 ||
                $i + $j == $n - 1)
                echo "*";
            else
                echo " ";
        }
        echo "\n";
    }
}
 
    // Driver code
    // n denotes size which should be odd
    $n = 7;
     
    // Function calling
    pattern($n);
 
// This code is contributed by mits
?>

Javascript

<script>
// Javascript program to print diagonal star patterns
 function pattern( n)
 {
  
        // Loop denoting rows
        for (let i = 0; i < n; i++) {
 
            // Loop denoting columns
            for (let j = 0; j < n; j++) {
 
                // Checking boundary conditions
                // and main diagonal and
                // secondary diagonal conditions
                if (i == 0 || j == 0 || i == j || i == n - 1 || j == n - 1 || i + j == n - 1)
                    document.write("*");
                else
                    document.write("  ");
            }
            document.write("<br/>");
        }
    }
 
    // Driver function
 
    // n denotes size which should be odd
    let n = 7;
 
    // Function calling
    pattern(n);
 
// This code is contributed by gauravrajput1
</script>

Producción : 
 

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

Complejidad temporal: O(n 2 )

Espacio auxiliar: O(1)
Para una entrada dada, este programa imprime el siguiente patrón. La entrada debe ser un número impar.
Ejemplos: 
 

Input : 9
Output :


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

A continuación se muestra el código que se imprime arriba del patrón: 
 

C++

// CPP program to print diagonal pattern
#include <iostream>
using namespace std;
 
void pattern(int n)
{
    // For printing upper portion
    int c1 = (n - 1) / 2;
     
    // For printing lower portion
    int c2 = 3 * n / 2 - 1;
     
    // Loop denoting rows
    for (int i = 0; i < n; i++) {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking conditions for printing pattern
            if (i + j == c1 || i - j == c1 || j - i == c1
                        || i + j == c2 || i == c1 || j == c1)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
    // n denotes size
    int n = 9;
    // Function calling
    pattern(n);
    return 0;
}

Java

// Java program to print diagonal star patterns
 
import java.util.*;
import java.lang.*;
 
public class GfG{
    public static void pattern(int n)
    {
        // For printing upper portion
        int c1 = (n - 1) / 2;
     
        // For printing lower portion
        int c2 = 3 * n / 2 - 1;
     
        // Loop denoting rows
        for (int i = 0; i < n; i++) {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++) {
             
            // Checking conditions for printing
            // pattern
            if (i + j == c1 || i - j == c1
                || j - i == c1 || i + j == c2 ||
                i == c1 || j == c1)
                    System.out.print("*");
            else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
     
    // Driver function
    public static void main(String argc[]){
 
        // n denotes size which should be odd
        int n = 9;
 
        // Function calling
        pattern(n);
    }
     
}
 
// This code is contributed by Sagar Shukla

Python3

# Python 3 program to print
# diagonal pattern
 
def pattern(n) :
     
    # For printing upper portion
    c1 = (n - 1) // 2
     
    # For printing lower portion
    c2 = 3 * n // 2 - 1
     
    # Loop denoting rows
    for i in range(0 , n) :
        # Loop denoting columns
        for j in range(0 , n) :
             
            # Checking conditions for
            # printing pattern
            if (i + j == c1 or i - j == c1 or
                j - i == c1 or i + j == c2 or
                i == c1 or j == c1) :
                print( "*",end = "")
            else :
                print(" ",end = "")
         
        print("")
     
     
 
# Driver code
 
# n denotes size
n = 9
 
# Function calling
pattern(n)
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to print
// diagonal star patterns
using System;
 
class GfG
{
    public static void pattern(int n)
    {
        // For printing
        // upper portion
        int c1 = (n - 1) / 2;
     
        // For printing
        // lower portion
        int c2 = 3 * n / 2 - 1;
     
        // Loop denoting rows
        for (int i = 0; i < n; i++)
        {
         
        // Loop denoting columns
        for (int j = 0; j < n; j++)
        {
             
            // Checking conditions for
            // printing pattern
            if (i + j == c1 || i - j == c1 ||
                j - i == c1 || i + j == c2 ||
                    i == c1 || j == c1)
                    Console.Write("*");
            else
                Console.Write(" ");
        }
            Console.WriteLine();
        }
    }
     
    // Driver Code
    public static void Main()
    {
 
        // n denotes size which
        // should be odd
        int n = 9;
 
        // Function calling
        pattern(n);
    }
}
 
// This code is contributed by anuj_67.

PHP

<?php
// php program to print
// diagonal pattern
 
function pattern($n)
{
     
    // For printing upper portion
    $c1 = floor(($n - 1) / 2);
     
    // For printing lower portion
    $c2 = floor(3 * $n / 2 - 1);
     
    // Loop denoting rows
    for ($i = 0; $i < $n; $i++)
    {
         
        // Loop denoting columns
        for ($j = 0; $j < $n; $j++)
        {
             
            // Checking conditions for
            // printing pattern
            if ($i + $j == $c1 || $i - $j == $c1 ||
                $j - $i == $c1 || $i + $j == $c2 ||
                $i == $c1 || $j == $c1)
                echo "*";
            else
                echo " ";
        }
        echo "\n";
    }
}
 
    // Driver code
    // n denotes size
    $n = 9;
     
    // Function calling
    pattern($n);
     
// This code is contributed by mits
?>

Javascript

<script>
// javascript program to print diagonal star patterns
 
 
    function pattern(n) {
        // For printing upper portion
        var c1 = (n - 1) / 2;
 
        // For printing lower portion
        var c2 = parseInt(3 * n / 2 )- 1;
 
        // Loop denoting rows
        for (var i = 0; i < n; i++) {
 
            // Loop denoting columns
            for (var j = 0; j < n; j++) {
 
                // Checking conditions for printing
                // pattern
                if (i + j == c1 || i - j == c1 || j - i == c1 || i + j == c2 || i == c1 || j == c1)
                    document.write(" *");
                else
                    document.write("   ");
            }
            document.write("<br/>");
        }
    }
 
    // Driver function
        // n denotes size which should be odd
        var n = 9;
 
        // Function calling
        pattern(n);
 
// This code contributed by gauravrajput1
</script>

Producción : 
 

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

Complejidad temporal: O(n 2 )

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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