在Java中查詢所有行與所有列的和的差值


在Java中,矩陣可以用二維陣列表示。矩陣用於儲存和操作具有表格結構的資料。矩陣具有與其相關的多個屬性和運算,例如加法、減法、乘法、轉置和行列式計算。

根據題意,我們需要對所有單個行和列求和。然後我們需要計算這些行和列的和的差值,並顯示結果。

讓我們開始吧!

例如

假設原始矩陣為

{4, 2, 1},
{3, 5, 6},
{8, 9, 7}

對矩陣進行運算後,結果將為

所有行之和與所有列之和的差值是:0

演算法

步驟 1:定義並初始化矩陣。

步驟 2:計算每一行的和,並將結果儲存在一個數組中。

步驟 3:計算每一列的和,並將結果儲存在一個數組中。

步驟 4:計算所有行的和以及所有列的和。

步驟 5:求所有行的和與所有列的和的差值。

步驟 6:列印結果。

多種方法

我們提供了多種方法的解決方案。

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

  • 使用使用者自定義方法

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

方法 1:使用陣列元素的靜態初始化

在這種方法中,矩陣元素將在程式中初始化。然後根據演算法查詢所有行和所有列的和的差值。

示例

public class Main 
{
   public static void main(String[] args) 
   {
      // Define and initialize the matrix
      int[][] matrix = {{11, 7, 1},{15, 8, 6},{2, 5, 17}};
      // Calculate the sum of each row and store the results in an array
      int[] rowSums = new int[matrix.length];
      for (int i = 0; i < matrix.length; i++) {
         int sum = 0;
         for (int j = 0; j < matrix[i].length; j++) {
            sum += matrix[i][j];
         }
         rowSums[i] = sum;
      }
      // Calculate the sum of each column and store the results in an array
      int[] colSums = new int[matrix[0].length];
      for (int j = 0; j < matrix[0].length; j++) {
         int sum = 0;
         for (int i = 0; i < matrix.length; i++) {
            sum += matrix[i][j];
         }
         colSums[j] = sum;
      }
      // sum of all rows 
      int sumOfRows = 0;
      for (int sum : rowSums) {
         sumOfRows += sum;
      }
      // sum of all columns
      int sumOfCols = 0;
      for (int sum : colSums) {
         sumOfCols += sum;
      }
      // Finding the difference between the sum of all rows and the sum of all columns
      int difference = sumOfRows - sumOfCols;
      // Display the difference
      System.out.println("Difference between sum of all rows and all columns is: " + difference);
   }
}

輸出

Difference between sum of all rows and all columns is: 0

方法 2:使用使用者自定義方法

在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為引數傳遞給使用者自定義方法,並在方法內部根據演算法查詢所有行和所有列的和的差值。

示例

public class Main 
{
   public static void main(String[] args) 
   {
      // Define and initialize the matrix
      int[][] matrix = {{11, 7, 1},{15, 8, 6},{2, 5, 17}};
      // calling user defined method
      callingMatrix(matrix);
   }
   // user defined method	
   public static void callingMatrix(int[][] matrix) 
   {
      // Calculate the sum of each row and store the results in an array
      int[] rowSums = new int[matrix.length];
      for (int i = 0; i < matrix.length; i++) {
         int sum = 0;
         for (int j = 0; j < matrix[i].length; j++) {
            sum += matrix[i][j];
         }
         rowSums[i] = sum;
      }
      // Calculate the sum of each column and store the results in an array
      int[] colSums = new int[matrix[0].length];
      for (int j = 0; j < matrix[0].length; j++) {
         int sum = 0;
         for (int i = 0; i < matrix.length; i++) {
            sum += matrix[i][j];
         }
         colSums[j] = sum;
      }
      // sum of all rows 
      int sumOfRows = 0;
      for (int sum : rowSums) {
         sumOfRows += sum;
      }
      // sum of all columns
      int sumOfCols = 0;
      for (int sum : colSums) {
         sumOfCols += sum;
      }
      // Finding the difference between the sum of all rows and the sum of all columns
      int difference = sumOfRows - sumOfCols;
      // Display the difference
      System.out.println("Difference between sum of all rows and all columns is: " + difference);
   }
}

輸出

Difference between sum of all rows and all columns is: 0

在本文中,我們探討了如何使用 Java 程式語言查詢所有行和所有列的和的差值。

更新於:2023年8月17日

瀏覽量 104 次

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告