patrón mágico

Dado un número entero N como entrada, la tarea es imprimir el patrón mágico como se indica a continuación:
 

n . 3 2 1 2 3 . . n 
_ . . . . . . . . . . 
3 3 3 3 2 1 2 3 3 3 3 
2 2 2 2 2 1 2 2 2 2 2 
1 1 1 1 1 1 1 1 1 1 1 
2 2 2 2 2 1 2 2 2 2 2 
3 3 3 3 2 1 2 3 3 3 3 
. . . . . . . . . . . 
n . 3 2 1 2 3 . . norte 
 

Ejemplos: 
 

Input: 3
Output: 
        3 2 1 2 3 
        2 2 1 2 2  
        1 1 1 1 1 
        2 2 1 2 2 
        3 2 1 2 3 

Input: 4
Output: 
        4 3 2 1 2 3 4                                                                   
        3 3 2 1 2 3 3                                                                   
        2 2 2 1 2 2 2                                                                   
        1 1 1 1 1 1 1                                                                   
        2 2 2 1 2 2 2                                                                   
        3 3 2 1 2 3 3                                                                   
        4 3 2 1 2 3 4  

Acercarse: 
 

  1. Considere una entrada N = 3, por lo que la array superpuesta será de fila = 5 y columna = 5 (es decir, 2 * N – 1) con todas las entradas como cero.
  2. Defina tres variables start = N, inc = 0 y dec = 2 * N – 1 para la manipulación.
  3. Ejecute un ciclo hasta que el inicio se convierta en cero.
  4. La fila con aum o (dec – 1) o la columna con aum o (dec – 1) se rellena con el valor inicial.
    entonces al inicio=3 
    la array será 
    3 3 3 3 3 
    3 0 0 0 3 
    3 0 0 0 3 
    3 0 0 0 3 
    3 3 3 3 3 
     
  5. Disminuye dec, comienza e incrementa el valor de inc.
  6. Repita el paso 3
  7. El bucle se detendrá como inicio = 0 y se obtiene el patrón deseado

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

C++

// C++ program to print the magical pattern
 
#include <iostream>
using namespace std;
 
void overLapping(int N, int matrix[100][100])
{
 
    int max, inc = 0, dec, start;
 
    // The size of matrix
    max = 2 * N - 1;
 
    // Fixing the range
    dec = max;
 
    // Overlapping value
    start = N;
 
    while (start != 0) {
        for (int row = 0; row < max; row++) {
            for (int col = 0; col < max; col++) {
                if (row == inc
                    || row == dec - 1
                    || col == dec - 1
                    || col == inc) {
 
                    matrix[row][col] = start;
                }
            }
        }
        start--;
        inc++;
        dec--;
    }
 
    // return matrix;
}
 
void DisplayMatrix(int matrix[][100], int max)
{
    // To display the overlapping matrix
    for (int row = 0; row < max; row++) {
        for (int col = 0; col < max; col++) {
            cout <<" "<< matrix[row][col];
        }
        cout << endl;
    }
}
 
// Driver code
int main()
{
    int N;
 
    // Get the value of N
    N = 3;
 
    // Declaring the overlapping matrix
    int matrix[100][100];
 
    // Create the magical matrix
    overLapping(N, matrix);
 
    // Print the magical matrix
    DisplayMatrix(matrix, (2 * N - 1));
}

C

// C program to print the magical pattern
#include <stdio.h>
 
void overLapping(int N, int matrix[100][100])
{
 
    int max, inc = 0, dec, start;
 
    // The size of matrix
    max = 2 * N - 1;
 
    // Fixing the range
    dec = max;
 
    // Overlapping value
    start = N;
 
    while (start != 0) {
        for (int row = 0; row < max; row++) {
            for (int col = 0; col < max; col++) {
                if (row == inc
                    || row == dec - 1
                    || col == dec - 1
                    || col == inc) {
 
                    matrix[row][col] = start;
                }
            }
        }
        start--;
        inc++;
        dec--;
    }
 
    // return matrix;
}
 
void DisplayMatrix(int matrix[][100], int max)
{
    // To display the overlapping matrix
    for (int row = 0; row < max; row++) {
        for (int col = 0; col < max; col++) {
            printf("%d ", matrix[row][col]);
        }
        printf("\n");
    }
}
 
// Driver code
int main()
{
    int N = 3;
 
    // Declaring the overlapping matrix
    int matrix[100][100];
 
    // Create the magical matrix
    overLapping(N, matrix);
 
    // Print the magical matrix
    DisplayMatrix(matrix, (2 * N - 1));
}

Java

// Java program to print the magical pattern
 
class GFG {
 
    static void overLapping(int N, int matrix[][]) {
 
        int max, inc = 0, dec, start;
 
        // The size of matrix
        max = 2 * N - 1;
 
        // Fixing the range
        dec = max;
 
        // Overlapping value
        start = N;
 
        while (start != 0) {
            for (int row = 0; row < max; row++) {
                for (int col = 0; col < max; col++) {
                    if (row == inc
                            || row == dec - 1
                            || col == dec - 1
                            || col == inc) {
 
                        matrix[row][col] = start;
                    }
                }
            }
            start--;
            inc++;
            dec--;
        }
 
        // return matrix;
    }
 
    static void DisplayMatrix(int matrix[][], int max) {
        // To display the overlapping matrix
        for (int row = 0; row < max; row++) {
            for (int col = 0; col < max; col++) {
                System.out.printf("%d ", matrix[row][col]);
            }
            System.out.printf("\n");
        }
    }
 
// Driver code
    public static void main(String[] args) {
        int N = 3;
 
        // Declaring the overlapping matrix
        int matrix[][] = new int[100][100];
 
        // Create the magical matrix
        overLapping(N, matrix);
 
        // Print the magical matrix
        DisplayMatrix(matrix, (2 * N - 1));
    }
}
 
// This code is contributed by Rajput-JI

Python3

# Python3 program to print the magical pattern
 
def overLapping(N, matrix):
 
    inc = 0
 
    # The size of matrix
    Max = 2 * N - 1
 
    # Fixing the range
    dec = Max
 
    # Overlapping value
    start = N
 
    while (start != 0):
        for row in range(Max):
            for col in range(Max):
                if (row == inc or row == dec - 1 or
                    col == dec - 1 or col == inc):
                    matrix[row][col] = start
             
        start -= 1
        inc += 1
        dec -= 1
     
    # return matrix
 
def DisplayMatrix(matrix, Max):
 
    # To display the overlapping matrix
    for row in range(Max):
        for col in range(Max):
            print(matrix[row][col], end = " ")
         
        print()
     
# Driver code
 
# Get the value of N
N = 3
 
# Declaring the overlapping matrix
matrix = [[0 for i in range(100)]
             for i in range(100)]
 
# Create the magical matrix
overLapping(N, matrix)
 
# Print the magical matrix
DisplayMatrix(matrix, (2 * N - 1))
 
# This code is contributed by
# Mohit Kumar 29

C#

// C# program to print the magical pattern
using System;
 
class GFG
{
public static void overLapping(int N,
                               int[,] matrix)
{
    int max, inc = 0, dec, start;
    max = 2 * N - 1;
    dec = max;
    start = N;
 
    while (start != 0)
    {
        for (int row = 0; row < max; row++)
        {
            for (int col = 0; col < max; col++)
            {
                if (row == inc || row == dec - 1 ||
                    col == dec - 1 || col == inc)
                {
                    matrix[row, col] = start;
                }
            }
        }
        start--;
        inc++;
        dec--;
    }
}
 
public static void DisplayMatrix(int[,] matrix, int max)
{
    for (int row = 0; row < max; row++)
    {
        for (int col = 0; col < max; col++)
        {
            Console.Write(" {0}", matrix[row, col]);
        }
        Console.Write("\n");
    }
}
 
// Driver Code
public static void Main()
{
    int N;
    N = 3;
 
    int[,] matrix = new int[100, 100];
 
    overLapping(N, matrix);
    DisplayMatrix(matrix, (2 * N - 1));
}
}
 
// This code is contributed by DrRoot_

PHP

<?php
// PHP program to print the magical pattern
 
function overLapping($N, &$matrix)
{
 
    $max; $inc = 0; $dec; $start;
 
    // The size of matrix
    $max = 2 * $N - 1;
 
    // Fixing the range
    $dec = $max;
 
    // Overlapping value
    $start = $N;
 
    while ($start != 0)
    {
        for ($row = 0; $row < $max; $row++)
        {
            for ($col = 0; $col < $max; $col++)
            {
                if ($row == $inc || $row == $dec - 1 ||
                    $col == $dec - 1 || $col == $inc)
                {
                    $matrix[$row][$col] = $start;
                }
            }
        }
        $start--;
        $inc++;
        $dec--;
    }
 
    // return matrix;
}
 
function DisplayMatrix($matrix, $max)
{
    // To display the overlapping matrix
    for ($row = 0; $row < $max; $row++)
    {
        for ($col = 0; $col < $max; $col++)
        {
            echo $matrix[$row][$col] . " ";
        }
        echo "\n";
    }
}
 
// Driver code
 
// Get the value of N
$N = 3;
 
// Declaring the overlapping matrix
$matrix = array();
 
// Create the magical matrix
overLapping($N, $matrix);
 
// Print the magical matrix
DisplayMatrix($matrix, (2 * $N - 1));
 
// This code is contributed
// by Akanksha Rai
?>

Javascript

<script>
 
// JavaScript program to print the magical pattern
 
    function overLapping(N,matrix)
    {
        let max, inc = 0, dec, start;
   
        // The size of matrix
        max = 2 * N - 1;
   
        // Fixing the range
        dec = max;
   
        // Overlapping value
        start = N;
   
        while (start != 0) {
            for (let row = 0; row < max; row++) {
                for (let col = 0; col < max; col++) {
                    if (row == inc
                            || row == dec - 1
                            || col == dec - 1
                            || col == inc) {
   
                        matrix[row][col] = start;
                    }
                }
            }
            start--;
            inc++;
            dec--;
        }
   
        // return matrix;
    }
     
    function DisplayMatrix(matrix,max)
    {
        // To display the overlapping matrix
        for (let row = 0; row < max; row++) {
            for (let col = 0; col < max; col++) {
                document.write( matrix[row][col]+" ");
            }
            document.write("<br>");
        }
    }
     
    // Driver code
    let N = 3;
    // Declaring the overlapping matrix
    let matrix = new Array(100);
    for(let i=0;i<100;i++)
    {
        matrix[i]=new Array(100);
    }
 
    // Create the magical matrix
    overLapping(N, matrix);
 
    // Print the magical matrix
    DisplayMatrix(matrix, (2 * N - 1));
 
 
     
 
// This code is contributed by patel2127
 
</script>
Producción: 

3 2 1 2 3 
2 2 1 2 2 
1 1 1 1 1 
2 2 1 2 2 
3 2 1 2 3

 

Publicación traducida automáticamente

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