本文介绍了使用递归方法将两个2D数组相乘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用递归方法将两个2D数组相乘。我写了这段代码

I am trying to multiply two 2D arrays using a recursive method. I wrote this code

   public static int mul(int i, int j, int[][]A, int[][] B){
    if(i>(A.length-1) && j>(A[i].length-1)) return 0;
    int x = A[i][j]*B[i][j];
    System.out.println(x);
    return mul(++i, ++j, A, B);

}

  public static void main(String[] args){
    Scanner k= new Scanner(System.in);
    int[][] A = new int[2][2];
    int[][] B = new int[2][2];


    for(int i=0;i<A.length;i++){
        for (int j=0;j<(A[0].length); j++){
            A[i][j]=k.nextInt();

        }

    }
    for(int i=0;i<B.length;i++){
        for (int j=0;j<(B[0].length); j++){
            B[i][j]=k.nextInt();

        }

    }

    mul(0, 0, A, B);
    }

但我收到此错误消息:

java.lang.ArrayIndexOutOfBoundsException:2

java.lang.ArrayIndexOutOfBoundsException: 2

在recursive.mul(recursive.java:5)

at recursive.mul(recursive.java:5)

在recursive.mul(recursive.java:7)

at recursive.mul(recursive.java:7)

在recursive.mul(recursive.java:7)

at recursive.mul(recursive.java:7)

在recursive.main(recursive.java:32)

at recursive.main(recursive.java:32)

非常感谢!

推荐答案

在递归矩阵乘法中,我们通过递归调用实现了三个迭代循环。最内在的multiplicatMatrix()递归调用是迭代k(col1或row2)。第二个递归调用multipleMatrix()用于更改列,最外层的递归调用用于更改行。

In Recursive Matrix Multiplication, we implement three loops of Iteration through recursive calls. The inner most Recursive call of multiplyMatrix() is to iterate k (col1 or row2). The second recursive call of multiplyMatrix() is to change the columns and the outermost recursive call is to change rows.

下面是递归矩阵乘法代码。

Below is Recursive Matrix Multiplication code.

代码:

// Java recursive code for Matrix Multiplication

class GFG
{
    public static int MAX = 100;

    // Note that below variables are static
    // i and j are used to know current cell of
    // result matrix C[][]. k is used to know
    // current column number of A[][] and row
    // number of B[][] to be multiplied
    public static int i = 0, j = 0, k = 0;

    static void multiplyMatrixRec(int row1, int col1, int A[][],
                       int row2, int col2, int B[][],
                       int C[][])
    {
        // If all rows traversed
        if (i >= row1)
            return;

        // If i < row1
        if (j < col2)
        {
            if (k < col1)
            {
                C[i][j] += A[i][k] * B[k][j];
                k++;

                multiplyMatrixRec(row1, col1, A, row2, col2, B, C);
            }

            k = 0;
            j++;
            multiplyMatrixRec(row1, col1, A, row2, col2, B, C);
        }

        j = 0;
        i++;
        multiplyMatrixRec(row1, col1, A, row2, col2, B, C);
    }

    // Function to multiply two matrices A[][] and B[][]
    static void multiplyMatrix(int row1, int col1, int A[][],
                    int row2, int col2, int B[][])
    {
        if (row2 != col1)
        {
            System.out.println("Not Possible\n");
            return;
        }

        int[][] C = new int[MAX][MAX];

        multiplyMatrixRec(row1, col1, A, row2, col2, B, C);

        // Print the result
        for (int i = 0; i < row1; i++)
        {
            for (int j = 0; j < col2; j++)
                System.out.print(C[i][j]+" ");

            System.out.println();
        }
    }

    // driver program
    public static void main (String[] args)
    {
        int row1 = 3, col1 = 3, row2 = 3, col2 = 3;
        int A[][] = { {1, 2, 3},
                      {4, 5, 6},
                      {7, 8, 9}};

        int B[][] = { {1, 2, 3},
                      {4, 5, 6},
                      {7, 8, 9} };

        multiplyMatrix(row1, col1, A, row2, col2, B);
    }
}

来源:

这篇关于使用递归方法将两个2D数组相乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:59