在Java中查詢矩陣首行和末行元素之和的乘積


矩陣不僅僅是資料的集合,它更是一種以二維矩形佈局排列的資料元素的集合。在Java中,二維陣列可以被認為是一個矩陣。

根據題目要求,任務是找到矩陣中首行和末行所有元素之和的乘積。

讓我們深入研究這篇文章,瞭解如何使用Java程式語言來完成這個任務。

舉幾個例子

示例1

假設原始矩陣是

{{15,5,9},
{4,14,16},
{7,8,19}};
  • 在找到矩陣中首行和末行所有元素之和的乘積後,結果索引將為:

  • 首行和末行所有元素之和的乘積是:986

示例2

假設原始矩陣是

{{11,7,2},
{3,4,24},
{12,8,15}}; 
  • 在找到矩陣中首行和末行所有元素之和的乘積後,結果索引將為:

  • 首行和末行所有元素之和的乘積是:700

演算法

  • 步驟1 - 初始化並宣告矩陣。

  • 步驟2 - 初始化並宣告首行和末行元素為0。

  • 步驟3 - 使用for迴圈計算首行和末行元素的和。

  • 步驟4 - 找到首行和末行所有元素的總和。

  • 步驟5 - 找到兩者之間的乘積。

  • 步驟6 - 列印結果。

多種方法

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

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

  • 使用使用者自定義方法

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

方法1:使用矩陣的靜態初始化

在這種方法中,矩陣元素將在程式中初始化。然後根據演算法找到首行和末行所有元素之和的乘積。

示例

public class Main{
   
   //main method
   public static void main(String args[]){
      
      //Initialising and declaring matrix
      int arr[][] = {{15,5,9},
         {4,14,16},
         {7,8,19}};
      int row, col ;
      
      //Initialising and declaring the first row and last row as 0
      int first_Row_Sum=0;
      int last_Row_Sum=0;
      
      //for loop to calculate the sum of first row elements and last row elements
      for(row=0;row<3;row++){
         for(col=0;col<3;col++){
            
            //finding sum of all elements of the first row
            if(row==0)
               first_Row_Sum = first_Row_Sum+arr[0][col];
            //finding sum of all elements of the last row
            else if(row==2)
               last_Row_Sum = last_Row_Sum+arr[2][col];
         }   
      }
      
      //finding product between sum of first row elements and last row elements
      int prod = first_Row_Sum * last_Row_Sum;
      
      //Printing the product of first row elements and last row elements 
      System.out.print("Product of sum of all elements in first row and last row is: "+prod);
   }
}

輸出

Product of sum of all elements in first row and last row is: 986

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

在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為引數傳遞給使用者自定義方法,並在方法內部根據演算法找到首行和末行所有元素之和的乘積。

示例

public class Main{
   //main method
   public static void main(String args[]){
      
      //Initialising and declaring matrix
      int arr[][] = {{11,7,2},
         {3,4,24},
         {12,8,15}};
      func(arr);               
   }         
   
   //user defined method
   static void func(int arr[][]){
      int row, col ;
      
      //Initialising and declaring the first row and last row as 0
      int first_Row_Sum=0;
      int last_Row_Sum=0;
      
      //for loop to calculate the sum of first row elements and last row elements
      for(row=0;row<3;row++){
         for(col=0;col<3;col++){
            
            //finding sum of all elements of the first row
               if(row==0)
                  first_Row_Sum = first_Row_Sum+arr[0][col];
            //finding sum of all elements of the last row
               else if(row==2)
                  last_Row_Sum = last_Row_Sum+arr[2][col];
         }   
      }
      
      //finding product between sum of first row elements and last row elements
      int prod = first_Row_Sum * last_Row_Sum;
      
      //Printing the product of first row elements and last row elements 
      System.out.print("Product of sum of all elements in first row and last row is: "+prod);
   }
}

輸出

Product of sum of all elements in first row and last row is: 700

在這篇文章中,我們探討了使用Java程式語言檢查對角佔優矩陣的不同方法。

更新於:2023年5月4日

467 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.