原文:

给你一个有序矩阵 n*n 。任务是通过将给定矩阵的镜像与矩阵本身相加来找到结果矩阵。 示例 :

input : mat[][] = {{1, 2, 3},
                              {4, 5, 6},
                              {7, 8, 9}}
output : 4 4 4
               10 10 10
               16 16 16
explanation:  
resultant matrix = {{1, 2, 3},      {{3, 2, 1}, 
                                 {4, 5, 6},       {6, 5, 4},
                                 {7, 8, 9}}       {9, 8, 7}}
input : mat[][] = {{1, 2},
                               {3, 4}}
output : 3 3
               7 7

当找到矩阵的镜像时,每个元素的行将保持不变,但其列的值将重新排列。对于任何元素 a ij ,其在镜像中的新位置将是 a i(n-j) 。获得矩阵的镜像后,将其添加到原始矩阵并打印结果。 积分照顾:

  1. 矩阵的索引将从 0,0 开始,在 n-1,n-1 结束,因此任何元素 a ij 的位置将是 a i(n-1-j)。
  2. 打印结果时,请注意正确的输出格式

以下是上述方法的实现:

c

// c   program to find sum of matrix and
// its mirror image
#include 
#define n 4
using namespace std;
// function to print the resultant matrix
void printsum(int mat[][n])
{
    for (int i = 0; i < n; i  ) {
        for (int j = 0; j < n; j  ) {
            cout << setw(3) << mat[i][n - 1 - j]   mat[i][j] << " ";
        }
        cout << "\n";
    }
}
// driver code
int main()
{
    int mat[n][n] = { { 2, 4, 6, 8 },
                      { 1, 3, 5, 7 },
                      { 8, 6, 4, 2 },
                      { 7, 5, 3, 1 } };
    printsum(mat);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to find sum of
// matrix and its mirror image
import java.io.*;
class gfg
{
static int n = 4;
// function to print the
// resultant matrix
static void printsum(int mat[][])
{
    for (int i = 0; i < n; i  )
    {
        for (int j = 0; j < n; j  )
        {
            system.out.print((mat[i][n - 1 - j]  
                              mat[i][j])   " ");
        }
        system.out.println();
    }
}
// driver code
public static void main (string[] args)
{
    int mat[][] = { { 2, 4, 6, 8 },
                    { 1, 3, 5, 7 },
                    { 8, 6, 4, 2 },
                    { 7, 5, 3, 1 } };
    printsum(mat);
}
}
// this code is contributed by anuj_67

python 3

# python 3 program to find sum of matrix
# and its mirror image
n = 4
# function to print the resultant matrix
def printsum(mat):
    for i in range(n):
        for j in range(n):
            print('{:>3}'.format(mat[i][n - 1 - j]  
                                 mat[i][j]), end =" ")
        print("\n", end = "")
# driver code
if __name__ == '__main__':
    mat = [[2, 4, 6, 8],
           [1, 3, 5, 7],
           [8, 6, 4, 2],
           [7, 5, 3, 1]]
    printsum(mat)
# this code is contributed by
# surendra_gangwar

c

// c# program to find sum of
// matrix and its mirror image
using system;
class gfg
{
static int n = 4;
// function to print the
// resultant matrix
static void printsum(int [,]mat)
{
    for (int i = 0; i < n; i  )
    {
        for (int j = 0; j < n; j  )
        {
            console.write((mat[i, n - 1 - j]  
                           mat[i, j])   " ");
        }
        console.writeline();
    }
}
// driver code
public static void main ()
{
    int [,]mat = { { 2, 4, 6, 8 },
                   { 1, 3, 5, 7 },
                   { 8, 6, 4, 2 },
                   { 7, 5, 3, 1 } };
    printsum(mat);
}
}
// this code is contributed by shs..

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


output: 

10  10  10  10 
  8   8   8   8 
 10  10  10  10 
  8   8   8   8