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