📌  相关文章
📜  求矩阵的所有边界和对角线元素的总和

📅  最后修改于: 2021-05-17 21:30:52             🧑  作者: Mango

给定NxN阶的2D数组arr [] [] ,任务是找到给定arr [] []的对角线和边界元素中所有元素的总和。
例子:

方法:

  1. 遍历给定的2D数组有两个循环,一个循环用于行(例如i ),另一个循环用于列(例如j )。
  2. 如果i等于j(i + j)等于(列的大小– 1),则该元素对给定2D数组的对角线有贡献。
  3. 如果( ij等于0 )或( ij等于第– 1列的大小),则该元素将成为给定2D数组的边界元素。
  4. 满足以上两个条件的所有元素的总和即为所需的总和。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
 
#include "bits/stdc++.h"
using namespace std;
 
const int N = 4;
 
// Function to find the sum of all diagonal
// and Boundary elements
void diagonalBoundarySum(int arr[N][N])
{
 
    int requiredSum = 0;
 
    // Traverse arr[][]
    // Loop from i to N-1 for rows
    for (int i = 0; i < N; i++) {
 
        // Loop from j = N-1 for columns
        for (int j = 0; j < N; j++) {
 
            // Condition for diagonal
            // elements
            if (i == j || (i + j) == N - 1) {
                requiredSum += arr[i][j];
            }
 
            // Condition for Boundary
            // elements
            else if (i == 0 || j == 0
                     || i == N - 1
                     || j == N - 1) {
                requiredSum += arr[i][j];
            }
        }
    }
 
    // Print the final Sum
    cout << requiredSum << endl;
}
 
// Driver Code
int main()
{
    int arr[][4] = { { 1, 2, 3, 4 },
                     { 1, 2, 3, 4 },
                     { 1, 2, 3, 4 },
                     { 1, 2, 3, 4 } };
 
    diagonalBoundarySum(arr);
    return 0;
}


Java
// Java implementation of the above approach
import java.util.*;
 
class GFG{
    public static int N = 4;
     
    // Function to find the sum of all diagonal
    // and Boundary elements
    static void diagonalBoundarySum(int arr[][]){
        int requiredSum = 0;
         
        // Traverse arr[][]
        // Loop from i to N-1 for rows
        for (int i = 0; i < N; i++) {
     
            // Loop from j = N-1 for columns
            for (int j = 0; j < N; j++) {
     
                // Condition for diagonal
                // elements
                if (i == j || (i + j) == N - 1) {
                    requiredSum += arr[i][j];
                }
     
                // Condition for Boundary
                // elements
                else if (i == 0 || j == 0 || i == N - 1|| j == N - 1) {
                    requiredSum += arr[i][j];
                }
            }
        }
     
        // Print the final Sum
        System.out.println(requiredSum);
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int arr[][] = { { 1, 2, 3, 4 },{ 1, 2, 3, 4 },
                        { 1, 2, 3, 4 },{ 1, 2, 3, 4 } };
     
        diagonalBoundarySum(arr);
         
    }
}
 
// This code is contributed by AbhiThakur


Python3
# Python implementation of the above approach
 
N = 4;
 
# Function to find the sum of all diagonal
# and Boundary elements
def diagonalBoundarySum(arr):
    requiredSum = 0;
 
    # Traverse arr
    # Loop from i to N-1 for rows
    for i in range(N):
 
        # Loop from j = N-1 for columns
        for j in range(N):
 
            # Condition for diagonal
            # elements
            if (i == j or (i + j) == N - 1):
                requiredSum += arr[i][j];
             
            # Condition for Boundary
            # elements
            elif(i == 0 or j == 0 or i == N - 1 or j == N - 1):
                requiredSum += arr[i][j];
 
    # Prthe final Sum
    print(requiredSum);
 
 
# Driver Code
if __name__ == '__main__':
    arr = [[ 1, 2, 3, 4 ],
    [ 1, 2, 3, 4 ],
    [ 1, 2, 3, 4 ],
    [ 1, 2, 3, 4 ]];
 
    diagonalBoundarySum(arr);
 
# This code is contributed by 29AjayKumar


C#
// C# implementation of the above approach
using System;
 
class GFG
{
    public static int N = 4;
     
    // Function to find the sum of all diagonal
    // and Boundary elements
    static void diagonalBoundarySum(int[, ] arr){
        int requiredSum = 0;
         
        // Traverse arr[][]
        // Loop from i to N-1 for rows
        for (int i = 0; i < N; i++) {
     
            // Loop from j = N-1 for columns
            for (int j = 0; j < N; j++) {
     
                // Condition for diagonal
                // elements
                if (i == j || (i + j) == N - 1) {
                    requiredSum += arr[i,j];
                }
     
                // Condition for Boundary
                // elements
                else if (i == 0 || j == 0 || i == N - 1|| j == N - 1) {
                    requiredSum += arr[i,j];
                }
            }
        }
     
        // Print the final Sum
        Console.WriteLine(requiredSum);
    }
     
    // Driver Code
    public static void Main()
    {
        int[, ] arr = { { 1, 2, 3, 4 },{ 1, 2, 3, 4 },{ 1, 2, 3, 4 },{ 1, 2, 3, 4 } };
     
        diagonalBoundarySum(arr);
         
    }
}
 
// This code is contributed by abhaysingh290895


Javascript


输出:
40