在Java中查詢每一行和每一列的乘積


在Java中,陣列是一個物件。它是一種非原始資料型別,用於儲存相同資料型別的值。Java中的矩陣只不過是一個多維陣列,它表示多行和多列。

這裡我們給出了一個包含一組元素的矩陣,根據問題陳述,我們必須找到每一行元素和每一列元素的乘積。

在本文中,我們將瞭解如何使用Java程式語言來實現這一點。

舉幾個例子

示例-1

給定矩陣 =

21 	22	23
24	25	26
27	28	29
  • 每一行的乘積 -

    • 第1行的乘積為:10626

    • 第2行的乘積為:15600

    • 第3行的乘積為:21924

  • 每一列的乘積 -

    • 第1列的乘積為:13608

    • 第2列的乘積為:15400

    • 第3列的乘積為:17342

示例-2

給定矩陣 =

9 	2	2	4
1	7	2	6
2	2	4	3
1	4	7	8
  • 每一行的乘積 -

    • 第1行的乘積為:144

    • 第2行的乘積為:84

    • 第3行的乘積為:48

    • 第4行的乘積為:224

  • 每一列的乘積 -

    • 第1列的乘積為:18

    • 第2列的乘積為:112

    • 第3列的乘積為:112

    • 第4列的乘積為:576

示例-3

給定矩陣 =

1 	2	3
4	5	6
7	8	9
  • 每一行的乘積 -

    • 行的乘積為:6

    • 行的乘積為:120

    • 行的乘積為:504

  • 每一列的乘積 -

    • 第1列的乘積為:28

    • 第2列的乘積為:80

    • 第3列的乘積為:162

演算法

  • 步驟-1 - 宣告並初始化一個稱為inputMatrix的二維矩陣。

  • 步驟-2 - 遍歷矩陣的每一行,並計算每一行中元素的乘積。然後將每一行的乘積列印到控制檯。

  • 步驟-3 - 對列重複相同的邏輯,並計算每一列中元素的乘積。

  • 步驟-4 - 然後將每一列的乘積列印到控制檯,並顯示不同的訊息。

語法

Java中的Matrix.length方法返回給定矩陣的長度。

下面是指其語法 -

inputMatrix.length

其中,“inputMatrix”指的是給定的矩陣。

多種方法

我們提供了不同方法的解決方案。

  • 透過使用矩陣元素的靜態初始化

  • 透過使用使用者定義的方法

讓我們逐一檢視程式及其輸出。

方法-1:透過使用矩陣元素的靜態初始化

在這種方法中,矩陣元素將在程式中初始化。然後根據演算法計算該矩陣的行和列的元素的乘積。

示例

public class Main {
   public static void main(String[] args) {
      
      //declare and initialized a 2d matrix
      int[][] inputMatrix = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
      System.out.println("Product of each row:");
      
      //initiate the loop to find the rows product values
      for (int[] r : inputMatrix) {
         int prod = 1;
         for (int value : r) {
            prod *= value;
         }
         System.out.println("The product of the row is: " + prod);
      }
      System.out.println("\nProduct of each column:");
      
      //initiate the loop to find the columns product values
      for (int a = 0; a < inputMatrix[0].length; a++) {
         int prod = 1;
         for (int b = 0; b < inputMatrix.length; b++) {
            prod *= inputMatrix[b][a];
         }
         System.out.println("The product of the column " + (a + 1) + " is: " + prod);
      }
   }
}

輸出

Product of each row:
The product of the row is: 6
The product of the row is: 6
The product of the row is: 6

Product of each column:
The product of the column 1 is: 1
The product of the column 2 is: 8
The product of the column 3 is: 27

方法-2:透過使用使用者定義的方法

在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為引數傳遞來呼叫使用者定義的方法,並在方法內部根據演算法計算該矩陣的行和列的元素的乘積。

示例

public class Main {
   public static void main(String[] args) {
      int[][] inputMatrix = {{11, 22, 33}, {44, 55, 66}, {77, 88, 99}};
      
      //call the user-defined methods to print the product values
      findRowProduct(inputMatrix);
      findColumnProduct(inputMatrix);
   }
   
   //user-defined method to find the product of row elements
   private static void findRowProduct(int[][] mat) {
      System.out.println("Product of each row:");
      for (int[] r : mat) {
         int prod = 1;
         for (int value : r) {
            prod *= value;
         }
         System.out.println("The product of the row is: " + prod);
      }
   }
   
   //user-defined method to find the product of column elements
   private static void findColumnProduct(int[][] mat) {
      System.out.println("\nProduct of each column:");
      for (int a = 0; a < mat[0].length; a++) {
         int prod = 1;
         for (int b = 0; b < mat.length; b++) {
            prod *= mat[b][a];
         }
         System.out.println("The product of the column " + (a + 1) + " is: " + prod);
      }
   }
}

輸出

Product of each row:
The product of the row is: 7986
The product of the row is: 159720
The product of the row is: 670824

Product of each column:
The product of the column 1 is: 37268
The product of the column 2 is: 106480
The product of the column 3 is: 215622

在本文中,我們探索了使用Java程式語言找到矩陣中每一行和每一列元素乘積的不同方法。

更新於: 2023年5月4日

500 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.