Forma bobinas en una array.

Dado un entero positivo n que representa las dimensiones de una array de 4n x 4n con valores de 1 a n llenados de izquierda a derecha y de arriba a abajo. Forme dos bobinas a partir de la array e imprima las bobinas.

Ejemplos:  

Input  : n = 1;
Output : Coil 1 : 10 6 2 3 4 8 12 16 
         Coil 2 : 7 11 15 14 13 9 5 1
Explanation : Matrix is 
1  2  3  4 
5  6  7  8 
9  10 11 12 
13 14 15 16

Input  : n = 2;
Output : Coil 1 : 36 28 20 21 22 30 38 46 54 
                  53 52 51 50 42 34 26 18 10 
                  2 3 4 5 6 7 8 16 24 32 40 
                  48 56 64 
        Coil 2 : 29 37 45 44 43 35 27 19 11 12 
                 13 14 15 23 31 39 47 55 63 62 
                 61 60 59 58 57 49 41 33 25 17
                 9 1 

Los elementos totales en la array son 16n 2 . Todos los elementos se dividen en dos bobinas. Cada bobina tiene 8n 2 elementos. Hacemos dos arreglos de este tamaño. Primero llenamos elementos en la bobina 1 recorriéndolos en el orden dado. Una vez que hemos llenado los elementos en la bobina1, podemos obtener elementos de otra bobina2 usando la fórmula bobina2[i] = 16*n*n + 1 -coil1[i]. 

Implementación:

C++

// C++ program to print 2 coils of a
// 4n x 4n matrix.
#include<iostream>
using namespace std;
 
// Print coils in a matrix of size 4n x 4n
void printCoils(int n)
{
    // Number of elements in each coil
    int m = 8*n*n;
 
    // Let us fill elements in coil 1.
    int coil1[m];
 
    // First element of coil1
    // 4*n*2*n + 2*n;
    coil1[0] = 8*n*n + 2*n;
    int curr = coil1[0];
 
    int nflg = 1, step = 2;
 
    // Fill remaining m-1 elements in coil1[]
    int index = 1;
    while (index < m)
    {
        // Fill elements of current step from
        // down to up
        for (int i=0; i<step; i++)
        {
            // Next element from current element
            curr = coil1[index++] = (curr - 4*n*nflg);
            if (index >= m)
                break;
        }
        if (index >= m)
            break;
 
        // Fill elements of current step from
        // up to down.
        for (int i=0; i<step; i++)
        {
            curr = coil1[index++] = curr + nflg;
            if (index >= m)
                break;
        }
        nflg = nflg*(-1);
        step += 2;
    }
 
    /* get coil2 from coil1 */
    int coil2[m];
    for (int i=0; i<8*n*n; i++)
        coil2[i] = 16*n*n + 1 -coil1[i];
 
    // Print both coils
    cout << "Coil 1 : ";
    for(int i=0; i<8*n*n; i++)
        cout << coil1[i] << " ";
    cout << "\nCoil 2 : ";
    for (int i=0; i<8*n*n; i++)
        cout << coil2[i] << " ";
}
 
// Driver code
int main()
{
    int n = 1;
    printCoils(n);
    return 0;
}

Java

// Java program to print 2 coils
// of a 4n x 4n matrix.
 
class GFG {
     
    // Print coils in a matrix of size 4n x 4n
    static void printCoils(int n)
    {
        // Number of elements in each coil
        int m = 8 * n * n;
     
        // Let us fill elements in coil 1.
        int coil1[] = new int[m];
     
        // First element of coil1
        // 4*n*2*n + 2*n;
        coil1[0] = 8 * n * n + 2 * n;
        int curr = coil1[0];
     
        int nflg = 1, step = 2;
     
        // Fill remaining m-1 elements in coil1[]
        int index = 1;
        while (index < m)
        {
            // Fill elements of current step from
            // down to up
            for (int i = 0; i < step; i++)
                {
                    // Next element from current element
                    curr = coil1[index++] = (curr - 4 * n * nflg);
                    if (index >= m)
                    break;
                }
            if (index >= m)
                break;
         
            // Fill elements of current step from
            // up to down.
            for (int i = 0; i < step; i++)
            {
                curr = coil1[index++] = curr + nflg;
                if (index >= m)
                break;
            }
             
            nflg = nflg * (-1);
            step += 2;
        }
     
        /* get coil2 from coil1 */
        int coil2[] = new int[m];
        for (int i = 0; i < 8 * n * n; i++)
            coil2[i] = 16 * n * n + 1 - coil1[i];
     
        // Print both coils
        System.out.print("Coil 1 : ");
         
        for (int i = 0; i < 8 * n * n; i++)
            System.out.print(coil1[i] + " ");
         
        System.out.print("\nCoil 2 : ");
         
        for (int i = 0; i < 8 * n * n; i++)
            System.out.print(coil2[i] + " ");
    }
     
    // Driver code
    public static void main(String[] args)
    {
        int n = 1;
        printCoils(n);
    }
}
 
// This code is contributed by Anant Agarwal.

Python3

# Python3 program to print 2 coils of a
# 4n x 4n matrix.
 
# Print coils in a matrix of size 4n x 4n
def printCoils(n):
     
    # Number of elements in each coil
    m = 8*n*n
     
    # Let us fill elements in coil 1.
    coil1 = [0]*m
     
    # First element of coil1
    # 4*n*2*n + 2*n
    coil1[0] = 8*n*n + 2*n
     
    curr = coil1[0]
     
    nflg = 1
    step = 2
     
    # Fill remaining m-1 elements in coil1[]
    index = 1
    while (index < m):
         
        # Fill elements of current step from
        # down to up
        for i in range(step):
             
            # Next element from current element
            curr = coil1[index] = (curr - 4*n*nflg)
            index += 1
            if (index >= m):
                break
        if (index >= m):
            break
         
        # Fill elements of current step from
        # up to down.
        for i in range(step):
             
            curr = coil1[index] = curr + nflg
            index += 1
            if (index >= m):
                break
        nflg = nflg*(-1)
        step += 2
     
    #get coil2 from coil1 */
     
    coil2 = [0]*m
    i = 0
    while(i < 8*n*n):
        coil2[i] = 16*n*n + 1 -coil1[i]
        i += 1
    # Print both coils
    print("Coil 1 :", end = " ")
    i = 0
    while(i < 8*n*n):
        print(coil1[i], end = " ")
        i += 1
    print("\nCoil 2 :", end = " ")
    i = 0
    while(i < 8*n*n):
        print(coil2[i], end = " ")
        i += 1
 
# Driver code
 
n = 1
printCoils(n)
 
# This code is contributed by shubhamsingh10

C#

// C# program to print 2 coils
// of a 4n x 4n matrix.
using System;
 
class GFG {
     
    // Print coils in a matrix of
    // size 4n x 4n
    static void printCoils(int n)
    {
         
        // Number of elements in
        // each coil
        int m = 8 * n * n;
     
        // Let us fill elements in
        // coil 1.
        int [] coil1 = new int[m];
     
        // First element of coil1
        // 4*n*2*n + 2*n;
        coil1[0] = 8 * n * n + 2 * n;
        int curr = coil1[0];
     
        int nflg = 1, step = 2;
     
        // Fill remaining m-1 elements
        // in coil1[]
        int index = 1;
        while (index < m)
        {
             
            // Fill elements of current
            // step from down to up
            for (int i = 0; i < step; i++)
                {
                    // Next element from
                    // current element
                    curr = coil1[index++]
                    = (curr - 4 * n * nflg);
                     
                    if (index >= m)
                        break;
                }
                 
            if (index >= m)
                break;
         
            // Fill elements of current step
            // from up to down.
            for (int i = 0; i < step; i++)
            {
                curr = coil1[index++] = curr
                                     + nflg;
                if (index >= m)
                    break;
            }
             
            nflg = nflg * (-1);
            step += 2;
        }
     
        /* get coil2 from coil1 */
        int [] coil2 = new int[m];
         
        for (int i = 0; i < 8 * n * n; i++)
            coil2[i] = 16 * n * n + 1 - coil1[i];
     
        // Print both coils
        Console.Write("Coil 1 : ");
         
        for (int i = 0; i < 8 * n * n; i++)
            Console.Write(coil1[i] + " ");
         
        Console.Write("\nCoil 2 : ");
         
        for (int i = 0; i < 8 * n * n; i++)
            Console.Write(coil2[i] + " ");
    }
     
    // Driver code
    public static void Main()
    {
        int n = 1;
         
        printCoils(n);
    }
}
 
// This code is contributed by KRV.

PHP

<?php
// PHP program to print 2 coils of a
// 4n x 4n matrix.
 
// Print coils in a matrix of size 4n x 4n
function printCoils( $n)
{
     
    // Number of elements in each coil
    $m = 8 * $n * $n;
 
    // Let us fill elements in coil 1.
    $coil1 = array();
 
    // First element of coil1
    // 4*n*2*n + 2*n;
    $coil1[0] = 8 * $n * $n + 2 * $n;
    $curr = $coil1[0];
 
    $nflg = 1; $step = 2;
 
    // Fill remaining m-1 elements in coil1[]
    $index = 1;
    while ($index < $m)
    {
        // Fill elements of current step from
        // down to up
        for ( $i = 0; $i < $step; $i++)
        {
            // Next element from current element
            $curr = $coil1[$index++] =
                     ($curr - 4*$n*$nflg);
            if ($index >= $m)
                break;
        }
        if ($index >= $m)
            break;
 
        // Fill elements of current step from
        // up to down.
        for ( $i=0; $i<$step; $i++)
        {
            $curr = $coil1[$index++] =
                            $curr + $nflg;
            if ($index >= $m)
                break;
        }
        $nflg = $nflg * (-1);
        $step += 2;
    }
 
    /* get coil2 from coil1 */
    $coil2 = array();
     
    for ( $i = 0; $i < 8 * $n * $n; $i++)
        $coil2[$i] = 16 * $n * $n + 1 -$coil1[$i];
 
    // Print both coils
    echo "Coil 1 : ";
    for( $i = 0; $i < 8 * $n * $n; $i++)
    echo $coil1[$i] , " ";
     
    echo "\nCoil 2 : ";
    for ( $i = 0; $i < 8 * $n * $n; $i++)
        echo $coil2[$i] , " ";
}
 
// Driver code
$n = 1;
printCoils($n);
 
// This code is contributed by anuj_67.
?>

Javascript

<script>
    // Javascript program to print 2 coils
    // of a 4n x 4n matrix.
     
    // Print coils in a matrix of
    // size 4n x 4n
    function printCoils(n)
    {
           
        // Number of elements in
        // each coil
        let m = 8 * n * n;
       
        // Let us fill elements in
        // coil 1.
        let coil1 = new Array(m);
        coil1.fill(0);
       
        // First element of coil1
        // 4*n*2*n + 2*n;
        coil1[0] = 8 * n * n + 2 * n;
        let curr = coil1[0];
       
        let nflg = 1, step = 2;
       
        // Fill remaining m-1 elements
        // in coil1[]
        let index = 1;
        while (index < m)
        {
               
            // Fill elements of current
            // step from down to up
            for (let i = 0; i < step; i++)
                {
                    // Next element from
                    // current element
                    curr = coil1[index++]
                    = (curr - 4 * n * nflg);
                       
                    if (index >= m)
                        break;
                }
                   
            if (index >= m)
                break;
           
            // Fill elements of current step
            // from up to down.
            for (let i = 0; i < step; i++)
            {
                curr = coil1[index++] = curr
                                     + nflg;
                if (index >= m)
                    break;
            }
               
            nflg = nflg * (-1);
            step += 2;
        }
       
        /* get coil2 from coil1 */
        let coil2 = new Array(m);
        coil2.fill(0);
           
        for (let i = 0; i < 8 * n * n; i++)
            coil2[i] = 16 * n * n + 1 - coil1[i];
       
        // Print both coils
        document.write("Coil 1 : ");
           
        for (let i = 0; i < 8 * n * n; i++)
            document.write(coil1[i] + " ");
           
        document.write("</br>" + "Coil 2 : ");
           
        for (let i = 0; i < 8 * n * n; i++)
            document.write(coil2[i] + " ");
    }
     
    let n = 1;
           
      printCoils(n);
     
</script>
Producción

Coil 1 : 10 6 2 3 4 8 12 16 
Coil 2 : 7 11 15 14 13 9 5 1 

Complejidad de Tiempo: O(n 2 )
Espacio Auxiliar: O(n 2 )

Preguntado en Yahoo
Este artículo es una contribución de Roshni Agarwal . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks. 

Publicación traducida automáticamente

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