Llenar diagonal para hacer que la suma de cada fila, columna y diagonal sea igual a una array de 3×3

Dados 9 elementos en una array de 3 x 3 donde el valor de las diagonales es 0 . Necesitamos encontrar los valores en diagonal para que la suma de cada fila, columna y diagonal sea igual.
Ejemplos: 
 

Input: 
0 3 6 
5 0 5
4 7 0
Output: 
6 3 6
5 5 5
4 7 4
Explanation: 
    Now the value of the sum of 
    any row or column is 15

Input: 
0 4 4
4 0 4
4 4 0
Output: 
4 4 4
4 4 4
4 4 4

Acercarse: 
 

  • Digamos que la diagonal es x, y y z.
  • El valor de x será ( x 2, 3 + x 3, 2 ) / 2. 
  • El valor de z será ( x 1, 2 + x 2, 1 ) / 2.
  • El valor de y será ( x + z ) / 2. 

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

C++

// C++ program to implement
// the above problem
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to print the matrix
void print(int arr[3][3])
{
    int i = 0, j = 0;
 
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++)
            cout << arr[i][j] << " ";
        cout << endl;
    }
}
 
// Function to find the diagonal values
void find(int arr[3][3])
{
    arr[0][0] = (arr[1][2] + arr[2][1]) / 2;
    arr[2][2] = (arr[0][1] + arr[1][0]) / 2;
    arr[1][1] = (arr[0][0] + arr[1][1]) / 2;
 
    // Print the new matrix with diagonals
    cout << "Matrix with diagonals:\n";
    print(arr);
}
 
// Driver code
int main()
{
    // Initialize all the elements of a matrix
    int arr[3][3] = { { 0, 54, 48 },
                      { 36, 0, 78 },
                      { 66, 60, 0 } };
 
    cout << "Matrix initially:\n";
    print(arr);
 
    find(arr);
 
    return 0;
}

Java

// Java program to implement
// the above problem
class GFG
{
 
// Function to print the matrix
static void print(int arr[][])
{
    int i = 0, j = 0;
 
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
            System.out.print( arr[i][j] + " ");
        System.out.println();
    }
}
 
// Function to find the diagonal values
static void find(int arr[][])
{
    arr[0][0] = (arr[1][2] + arr[2][1]) / 2;
    arr[2][2] = (arr[0][1] + arr[1][0]) / 2;
    arr[1][1] = (arr[0][0] + arr[1][1]) / 2;
 
    // Print the new matrix with diagonals
    System.out.print( "Matrix with diagonals:\n");
    print(arr);
}
 
// Driver code
public static void main(String args[])
{
    // Initialize all the elements of a matrix
    int arr[][] = { { 0, 54, 48 },
                    { 36, 0, 78 },
                    { 66, 60, 0 } };
 
    System.out.print( "Matrix initially:\n");
    print(arr);
 
    find(arr);
}
}
 
// This code is contributed by Arnab Kundu

Python3

# Python3 program to implement
# the above problem
 
# Function to print the matrix
def print_(arr, n):
    for i in range(n):
        for j in range(n):
            print(arr[i][j], end = " ")
        print("\n", end= "")
 
# Function to find the diagonal values
def find(arr, n):
    arr[0][0] = (arr[1][2] + arr[2][1]) // 2
    arr[2][2] = (arr[0][1] + arr[1][0]) // 2
    arr[1][1] = (arr[0][0] + arr[1][1]) // 2
    print("\nMatrix with diagonals:")
    print_(arr, n)
 
# Driver code
arr = [[0, 54, 48],
       [36, 0, 78],
       [66, 60, 0]]
 
n = 3
print("Matrix initially:")
print_(arr, n)
find(arr, n)
 
# This code is contributed by Shrikant13

C#

// C# program to implement
// the above problem
using System;
 
class GFG
{
 
    // Function to print the matrix
    static void print(int [,]arr)
    {
        int i = 0, j = 0;
     
        for (i = 0; i < 3; i++)
        {
            for (j = 0; j < 3; j++)
                Console.Write( arr[i, j] + " ");
                 
            Console.WriteLine();
        }
    }
     
    // Function to find the diagonal values
    static void find(int [,]arr)
    {
        arr[0, 0] = (arr[1, 2] + arr[2, 1]) / 2;
        arr[2, 2] = (arr[0, 1] + arr[1, 0]) / 2;
        arr[1, 1] = (arr[0, 0] + arr[1, 1]) / 2;
     
        // Print the new matrix with diagonals
        Console.Write( "Matrix with diagonals:\n");
        print(arr);
    }
     
    // Driver code
    public static void Main()
    {
        // Initialize all the elements of a matrix
        int [,]arr = { { 0, 54, 48 },
                        { 36, 0, 78 },
                        { 66, 60, 0 } };
     
        Console.Write( "Matrix initially:\n");
        print(arr);
     
        find(arr);
    }
}
 
// This code is contributed by Ryuga

PHP

<?php
// PHP program to implement
// the above problem Function
// to print the matrix
function printt( $arr)
{
    $i = 0;
    $j = 0;
 
    for ($i = 0; $i < 3; $i++)
    {
        for ($j = 0; $j < 3; $j++)
            echo $arr[$i][$j], " ";
        echo "\n";
    }
}
 
// Function to find the diagonal values
function find( $arr)
{
    $arr[0][0] = ($arr[1][2] + $arr[2][1]) / 2;
    $arr[2][2] = ($arr[0][1] + $arr[1][0]) / 2;
    $arr[1][1] = ($arr[0][0] + $arr[1][1]) / 2;
 
    // Print the new matrix with diagonals
    echo "Matrix with diagonals:\n";
    printt($arr);
}
 
// Driver code
 
// Initialize all the elements of a matrix
$arr =array(array( 0, 54, 48 ),
        array( 36, 0, 78 ),
        array( 66, 60, 0 ));
 
echo "Matrix initially:\n";
printt($arr);
 
find($arr);
 
#This Code is contributed by ajit..
?>

Javascript

<script>
// Java script program to implement
// the above problem
 
// Function to print the matrix
function print(arr)
{
    let i = 0, j = 0;
 
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
            document.write( parseInt(arr[i][j]) + " ");
        document.write("<br>");
    }
}
 
// Function to find the diagonal values
function find(arr)
{
    arr[0][0] = (arr[1][2] + arr[2][1]) / 2;
    arr[2][2] = (arr[0][1] + arr[1][0]) / 2;
    arr[1][1] = (arr[0][0] + arr[1][1]) / 2;
 
    // Print the new matrix with diagonals
    document.write( "Matrix with diagonals:<br>");
    print(arr);
}
 
// Driver code
 
    // Initialize all the elements of a matrix
    let arr = [[ 0, 54, 48 ],
                    [36, 0, 78 ],
                    [ 66, 60, 0 ]];
 
    document.write( "Matrix initially:<br>");
    print(arr);
 
    find(arr);
 
// This code is contributed by sravan kumar Gottumukkala
</script>
Producción: 

Matrix initially:
0 54 48 
36 0 78 
66 60 0 

Matrix with diagonals:
69 54 48 
36 34 78 
66 60 45

 

Complejidad de tiempo: O(1), el código se ejecutará en un tiempo constante.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

Publicación traducida automáticamente

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