📌  相关文章
📜  用周围元素的总和替换矩阵的中间元素

📅  最后修改于: 2021-05-31 21:36:19             🧑  作者: Mango

给定奇数尺寸的方阵mat [] [] ,任务是将矩阵的最中间元素的值更改为周围元素的总和。

例子:

Input: 
mat[][] = { {2, 1, 7}, 
            {3, 7, 2}, 
            {5, 4, 9} }
Output:
2 1 7
3 10 2
5 4 9

Input: 
mat[][] = {{1, 3, 5, 6, 7},
           {3, 5, 3, 2, 1},
           {1, 2, 3, 4, 5},
           {7, 9, 2, 1, 6},
           {9, 1, 5, 3, 2} }
Output: 
1 3 5 6 7
3 5 3 2 1
1 2 11 4 5
7 9 2 1 6
9 1 5 3 2

方法:奇数阶矩阵的中间元素将位于位置mat [n / 2] [n / 2]处

因此,直接将元素更新为:

mat[n / 2][n / 2] = mat[n / 2 - 1][n / 2]
                  + mat[n / 2][n / 2 - 1]
                  + mat[n / 2 + 1][n / 2]
                  + mat[n / 2][n / 2 + 1]

下面是上述方法的实现:

C++
// C++ program to replace the value of the middle element 
// of the matrix with the sum of surrounding elements
  
#include 
using namespace std;
const int MAX = 100;
  
// Function to print the matrix
void print(int mat[][MAX], int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << mat[i][j] << " ";
        }
        cout << endl;
    }
}
  
// Function to change the value of the middle element 
// of the matrix to the sum of surrounding elements
void changemiddle(int mat[][MAX], int n)
{
    // Change the middle element
    mat[n / 2][n / 2]
        = mat[n / 2 - 1][n / 2]
          + mat[n / 2][n / 2 - 1]
          + mat[n / 2 + 1][n / 2]
          + mat[n / 2][n / 2 + 1];
  
    // Function call to print the matrix
    print(mat, n);
}
  
// Driver code
int main()
{
    int mat[][MAX] = { { 2, 1, 7 },
                       { 3, 7, 2 },
                       { 5, 4, 9 } };
  
    changemiddle(mat, 3);
  
    return 0;
}


Java
// Java program to replace the value of the middle element 
// of the matrix with the sum of surrounding elements\
  
public class GFG{
      
    // Function to print the matrix
    static void print(int mat[][], int n)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(mat[i][j] + " ");
            }
             System.out.println();
        }
    }
      
    // Function to change the value of the middle element 
    // of the matrix to the sum of surrounding elements
    static void changemiddle(int mat[][], int n)
    {
        // Change the middle element
        mat[n / 2][n / 2]
            = mat[n / 2 - 1][n / 2]
              + mat[n / 2][n / 2 - 1]
              + mat[n / 2 + 1][n / 2]
              + mat[n / 2][n / 2 + 1];
      
        // Function call to print the matrix
        print(mat, n);
    }
      
    // Driver code
    public static void main(String []args)
    {
        int mat[][] = { { 2, 1, 7 },
                           { 3, 7, 2 },
                           { 5, 4, 9 } };
      
        changemiddle(mat, 3);
    }
    // This code is contributed by Ryuga
}


Python3
# Python3 program to replace the value
# of the middle element of the matrix 
# with the sum of surrounding elements 
  
MAX = 100
  
# Function to print the matrix 
def printMatrix(mat, n): 
  
    for i in range(n): 
        for j in range(n): 
            print(mat[i][j], end = " ") 
          
        print()
  
# Function to change the value of 
# the middle element of the matrix
# to the sum of surrounding elements 
def changemiddle(mat, n):
  
    # Change the middle element 
    mat[n // 2][n // 2] = (mat[n // 2 - 1][n // 2] + 
                           mat[n // 2][n // 2 - 1] + 
                           mat[n // 2 + 1][n // 2] + 
                           mat[n // 2][n // 2 + 1])
  
    # Function call to print the matrix 
    printMatrix(mat, n) 
  
# Driver Code
if __name__ == "__main__": 
  
    mat = [ [ 2, 1, 7 ], 
            [ 3, 7, 2 ], 
            [ 5, 4, 9 ]] 
  
    changemiddle(mat, 3)
  
# This code is contributed 
# by rituraj_jain


C#
// C# program to replace the value of the middle element 
// of the matrix with the sum of surrounding elements\
   
using System;
public class GFG{
       
    // Function to print the matrix
    static void print(int[,] mat, int n)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                Console.Write(mat[i,j] + " ");
            }
             Console.Write("\n");
        }
    }
       
    // Function to change the value of the middle element 
    // of the matrix to the sum of surrounding elements
    static void changemiddle(int[,] mat, int n)
    {
        // Change the middle element
        mat[n / 2,n / 2]
            = mat[n / 2 - 1,n / 2]
              + mat[n / 2,n / 2 - 1]
              + mat[n / 2 + 1,n / 2]
              + mat[n / 2,n / 2 + 1];
       
        // Function call to print the matrix
        print(mat, n);
    }
       
    // Driver code
    public static void Main()
    {
        int[,] mat = { { 2, 1, 7 },
                           { 3, 7, 2 },
                           { 5, 4, 9 } };
       
        changemiddle(mat, 3);
    }
     
}


PHP


输出:
2 1 7 
3 10 2 
5 4 9

时间复杂度: O(n * m),这里n是行数,m是列数。

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”