📜  打印矩阵及其镜像的和

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

打印矩阵及其镜像的和

给定一个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(nj) 。得到矩阵的镜像后,将其添加到原始矩阵中并打印结果。
注意事项:

  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


Python3
# 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..


PHP


Javascript


输出:
10  10  10  10 
  8   8   8   8 
 10  10  10  10 
  8   8   8   8

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