📜  在方阵中找到对角线的收敛元素

📅  最后修改于: 2022-05-13 01:57:24.573000             🧑  作者: Mango

在方阵中找到对角线的收敛元素

给定一个方阵,任务是找到该方阵的左右对角线收敛的矩阵元素。
例子:

Input: n = 5, matrix = 
[ 1 2 3 4 5
  5 6 7 8 6
  9 5 6 8 7
  2 3 5 6 8
  1 2 3 4 5 ]
Output: 6

Input: n = 4, matrix = 
[ 1 2 3 4
  5 6 7 8
  9 0 1 2 
  4 5 6 1 ]
Output: NULL
Here there no converging element at all. 
Hence the answer is null.

方法:

  • 如果矩阵的行数和列数是偶数,那么我们只打印NULL,因为在行数和列数偶数的情况下不会有收敛元素。
  • 若矩阵的行数和列数为奇数,求n的中值为
mid = n/2
  • arr[mid][mid] 本身是收敛的对角线元素。

下面是上述方法的实现:

C++
// C++ program to find the converging element
// of the diagonals in a square matrix
 
#include 
#include 
#include 
 
// Driver code
int main()
{
    int n = 5;
    int a[][5] = { { 1, 2, 3, 4, 5 },
                   { 5, 6, 7, 8, 6 },
                   { 9, 5, 6, 8, 7 },
                   { 2, 3, 5, 6, 8 },
                   { 1, 2, 3, 4, 5 } };
 
    int convergingele, mid;
    int i, j;
 
    // If n is even, then convergence
    // element will be null.
    if (n % 2 == 0) {
        printf("NULL\n");
    }
 
    else {
        // finding the mid
        mid = n / 2;
 
        // finding the converging element
        convergingele = a[mid][mid];
 
        printf("%d\n", convergingele);
    }
}


Java
// Java program to find the converging element
// of the diagonals in a square matrix
class GFG
{
 
    // Driver code
    public static void main(String args[])
    {
        int n = 5;
        int a[][] = {{1, 2, 3, 4, 5},
                     {5, 6, 7, 8, 6},
                     {9, 5, 6, 8, 7},
                     {2, 3, 5, 6, 8},
                     {1, 2, 3, 4, 5}};
 
        int convergingele, mid;
        int i, j;
 
        // If n is even, then convergence
        // element will be null.
        if (n % 2 == 0)
        {
            System.out.printf("NULL\n");
        }
        else
        {
            // finding the mid
            mid = n / 2;
 
            // finding the converging element
            convergingele = a[mid][mid];
 
            System.out.printf("%d\n", convergingele);
        }
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to find the converging element
# of the diagonals in a square matrix
 
# Driver code
n = 5
a = [[ 1, 2, 3, 4, 5 ],
     [ 5, 6, 7, 8, 6 ],
     [ 9, 5, 6, 8, 7 ],
     [ 2, 3, 5, 6, 8 ],
     [ 1, 2, 3, 4, 5 ]]
 
# If n is even, then convergence
# element will be null.
if (n % 2 == 0):
    print("NULL")
else :
     
    # finding the mid
    mid = n // 2
 
    # finding the converging element
    convergingele = a[mid][mid]
 
    print(convergingele)
 
# This code is contributed by Mohit Kumar


C#
// C# iprogram to find the converging element
// of the diagonals in a square matrix
using System;
     
class GFG
{
 
    // Driver code
    public static void Main(String []args)
    {
        int n = 5;
        int [,]a = {{1, 2, 3, 4, 5},
                    {5, 6, 7, 8, 6},
                    {9, 5, 6, 8, 7},
                    {2, 3, 5, 6, 8},
                    {1, 2, 3, 4, 5}};
 
        int convergingele, mid;
 
        // If n is even, then convergence
        // element will be null.
        if (n % 2 == 0)
        {
            Console.Write("NULL\n");
        }
        else
        {
            // finding the mid
            mid = n / 2;
 
            // finding the converging element
            convergingele = a[mid,mid];
 
            Console.Write("{0}\n", convergingele);
        }
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
6

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程