Java程序计算矩阵对角线之和
在本文中,我们将了解如何计算矩阵对角线的和。矩阵的元素以行和列的形式排列。主对角线是指方形矩阵中从左上角到右下角的对角线。
副对角线是指方形矩阵中从左下角到右上角的对角线。
以下是同一演示 -
假设我们的输入是 -
The input matrix: 4 5 6 7 1 7 3 4 11 12 13 14 23 24 25 50
期望输出为 -
Sum principal diagonal elements: 74 Sum of secondary diagonal elements: 45
算法
Step 1 - START Step 2 - Declare an integer matrix namely input_matrix Step 3 - Define the values. Step 4 - Iterate over each element of the matrix using two for-loops, add the diagonal elements use the condition ‘i’ = ‘j’ to get the sum of principle diagonal elements and the condition ‘i’ + ‘j’ = matrix_size – 1 to get the sum of secondary diagonal elements. Step 5 - Display the result Step 5 - Stop
示例 1
在这里,我们将所有操作绑定在“main”函数下。
public class MatrixDiagonals {
static public void main(String[] args) {
int[][] input_matrix = {
{ 4, 5, 6, 7 },
{ 1, 7, 3, 4 },
{ 11, 12, 13, 14 },
{ 23, 24, 25, 50 }
};
int matrix_size = 4;
System.out.println("The matrix is defined as : ");
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++)
System.out.print( input_matrix[i][j] + " ");
System.out.print("\n");
}
int principal_diagonal = 0, secondary_diagonal = 0;
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
if (i == j)
principal_diagonal += input_matrix[i][j];
if ((i + j) == (matrix_size - 1))
secondary_diagonal += input_matrix[i][j];
}
}
System.out.println("\n The sum of principal diagonal elements of the matrix is: " + principal_diagonal);
System.out.println("\n The sum of secondary diagonal elements of the matrix is: " + secondary_diagonal);
}
}输出
The matrix is defined as : 4 5 6 7 1 7 3 4 11 12 13 14 23 24 25 50 The sum of principal diagonal elements of the matrix is: 74 The sum of secondary diagonal elements of the matrix is: 45
示例 2
在这里,我们将操作封装到函数中,展示了面向对象编程。
public class MatrixDiagonals {
static void diagonals_sum(int[][] input_matrix, int matrix_size) {
int principal_diagonal = 0, secondary_diagonal = 0;
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++) {
if (i == j)
principal_diagonal += input_matrix[i][j];
if ((i + j) == (matrix_size - 1))
secondary_diagonal += input_matrix[i][j];
}
}
System.out.println("\n The sum of principal diagonal elements of the matrix is: " + principal_diagonal);
System.out.println("\n The sum of secondary diagonal elements of the matrix is: " + secondary_diagonal);
}
static public void main(String[] args) {
int[][] input_matrix = {
{ 4, 5, 6, 7 },
{ 1, 7, 3, 4 },
{ 11, 12, 13, 14 },
{ 23, 24, 25, 50 }
};
int matrix_size = 4;
System.out.println("The matrix is defined as : ");
for (int i = 0; i < matrix_size; i++) {
for (int j = 0; j < matrix_size; j++)
System.out.print( input_matrix[i][j] + " ");
System.out.print("\n");
}
diagonals_sum(input_matrix, matrix_size);
}
}输出
The matrix is defined as: 4 5 6 7 1 7 3 4 11 12 13 14 23 24 25 50 The sum of principal diagonal elements of the matrix is:74 The sum of secondary diagonal elements of the matrix is: 45
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP