📜  将两个矩阵相加的Java程序

📅  最后修改于: 2021-09-06 05:18:02             🧑  作者: Mango

给定两个相同大小的矩阵AB ,在Java添加它们的任务。
例子:

Input: A[][] = {{1, 2}, 
                {3, 4}}
       B[][] = {{1, 1}, 
                {1, 1}}
Output: {{2, 3}, 
         {4, 5}}

Input: A[][] = {{2, 4}, 
                {3, 4}}
       B[][] = {{1, 2}, 
                {1, 3}}       
Output: {{3, 6}, 
         {4, 7}}

方法:

  • 取两个要相加的矩阵
  • 创建一个新的 Matrix 来存储两个矩阵的总和
  • 遍历两个矩阵的每个元素并将它们相加。将此总和存储在相应索引处的新矩阵中。
  • 打印最终的新矩阵

下面是上述方法的实现:

Java
// Java program for addition of two matrices
 
import java.io.*;
 
class GFG {
 
    // Function to print Matrix
    static void printMatrix(int M[][],
                            int rowSize,
                            int colSize)
    {
        for (int i = 0; i < rowSize; i++) {
            for (int j = 0; j < colSize; j++)
                System.out.print(M[i][j] + " ");
 
            System.out.println();
        }
    }
 
    // Function to add the two matrices
    // and store in matrix C
    static int[][] add(int A[][], int B[][],
                       int size)
    {
        int i, j;
        int C[][] = new int[size][size];
 
        for (i = 0; i < size; i++)
            for (j = 0; j < size; j++)
                C[i][j] = A[i][j] + B[i][j];
 
        return C;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int size = 4;
 
        int A[][] = { { 1, 1, 1, 1 },
                      { 2, 2, 2, 2 },
                      { 3, 3, 3, 3 },
                      { 4, 4, 4, 4 } };
        // Print the matrices A
        System.out.println("\nMatrix A:");
        printMatrix(A, size, size);
 
        int B[][] = { { 1, 1, 1, 1 },
                      { 2, 2, 2, 2 },
                      { 3, 3, 3, 3 },
                      { 4, 4, 4, 4 } };
        // Print the matrices B
        System.out.println("\nMatrix B:");
        printMatrix(B, size, size);
 
        // Add the two matrices
        int C[][] = add(A, B, size);
 
        // Print the result
        System.out.println("\nResultant Matrix:");
        printMatrix(C, size, size);
    }
}


输出:
Matrix A:
1 1 1 1 
2 2 2 2 
3 3 3 3 
4 4 4 4 

Matrix B:
1 1 1 1 
2 2 2 2 
3 3 3 3 
4 4 4 4 

Resultant Matrix:
2 2 2 2 
4 4 4 4 
6 6 6 6 
8 8 8 8

时间复杂度: O(N 2 ),其中 N 是矩阵的行数或列数

辅助空间: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live