Java 找到矩阵的第一行和最后一行元素之和的乘积

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编程语言探索了不同的方法来检查对角占优矩阵。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程