如何在Java中檢查矩陣是否為幻方?
矩陣不過是在二維的矩形佈局中排列的資料元素的集合。在Java中,二維陣列可以被視為矩陣。
根據問題陳述,任務是檢查矩陣是否為幻方。
如果任何行、列或對角線的元素之和等於一個特定數字,則稱該矩陣為幻方。
讓我們深入研究這篇文章,瞭解如何使用Java程式語言來實現。
向您展示一些例項
例項1
Suppose the original matrix is {
{ 13, 8, 15 },
{ 14, 12, 10 },
{ 9, 16, 11 }
};
這裡任何一行、任何一列或對角線的和都等於36。
在檢查幻方之後,結果索引將是
給定的矩陣是一個幻方
例項2
Suppose the original matrix is{
{ 8, 7, 6 },
{ 9, 5, 1 },
{ 5, 3, 8 }
};
這裡任何一行、任何一列或對角線的和都不相等。
在檢查幻方之後,結果索引將是
給定的矩陣不是一個幻方
演算法
步驟1 - 初始化並宣告矩陣
步驟2 - 宣告布林值以檢查幻方。
步驟3 - 使用迴圈查詢兩個對角線的和。
步驟4 - 使用for迴圈查詢行和列的和。
步驟5 - 檢查幻方。
步驟6 - 列印結果。
語法
要獲取陣列的長度(陣列中元素的數量),陣列有一個內建屬性,即length
下面是指其語法 -
array.length
其中,“陣列”指的是陣列引用。
多種方法
我們提供了不同方法的解決方案。
透過使用矩陣的靜態初始化
透過使用使用者定義的方法
讓我們逐一檢視程式及其輸出。
方法1:透過使用矩陣的靜態初始化
在這種方法中,矩陣元素將在程式中初始化。然後根據演算法檢查矩陣是否為幻方。
示例
public class Main {
//main method
public static void main(String[] args){
//Initialising and declaring matrix
int mat[][] = {{ 13, 8, 15 }, { 14, 12, 10 }, { 9, 16, 11 }};
int M = 3;
//declare boolean to check for magic square or not
boolean flag = false;
//Initialising and declaring the diagonal sum as 0
int sum1 = 0,sum2=0;
//finding the sum of the two diagonals i.e. sum1 and sum2
for (int i = 0; i < M; i++){
sum1 += mat[i][i];
sum2 += mat[i][M-1-i];
}
//check if sum of diagonals are unequal then it is not a magic square
if(sum1!=sum2)
flag = true;
for (int i = 0; i < M; i++) {
//Initialising and declaring the row sum and column sum as 0
int rowSum = 0, colSum = 0;
//finding the sum of the rows and columns i.e. row and column
for (int j = 0; j < M; j++){
rowSum += mat[i][j];
colSum += mat[j][i];
}
//check if sum of rows, columns and diagonals are unequal then it is not a magic square
if (rowSum != colSum || colSum != sum1)
flag = true;
}
//checking and printing magic square
if (!flag)
System.out.println("Given matrix is a Magic Square");
else
System.out.println("Given matrix is a not a magic" + " Square");
}
}
輸出
Given matrix is a Magic Square
方法2:透過使用使用者定義的方法
在這種方法中,矩陣元素將在程式中初始化。然後透過將矩陣作為引數傳遞來呼叫使用者定義的方法,並在方法內部根據演算法檢查矩陣是否為幻方。
示例
public class Main {
//main method
public static void main(String[] args){
//Initialising and declaring matrix
int mat[][] = {{ 8, 7, 6 }, { 9, 5, 1 }, { 5, 3, 8 }};
//calling user defined function
magicSquare(mat);
}
static int M = 3;
//user defined method
static void magicSquare(int mat[][]) {
//declare boolean to check for magic square or not
boolean flag = false;
//Initialising and declaring the diagonal sum as 0
int sum1 = 0,sum2=0;
//finding the sum of the two diagonals i.e. sum1 and sum2
for (int i = 0; i < M; i++) {
sum1 += mat[i][i];
sum2 += mat[i][M-1-i];
}
//check if sum of diagonals are unequal then it is not a magic square
if(sum1!=sum2)
flag = true;
for (int i = 0; i < M; i++) {
//Initialising and declaring the rows and columns sum as 0
int rowSum = 0, colSum = 0;
//finding the sum of the rows and columns i.e. row and column
for (int j = 0; j < M; j++) {
rowSum += mat[i][j];
colSum += mat[j][i];
}
//check if sum of rows, columns and diagonals are unequal then it is not a magic square
if (rowSum != colSum || colSum != sum1)
flag = true;
}
//checking and printing magic square
if (!flag)
System.out.println("Given matrix is a Magic Square");
else
System.out.println("Given matrix is a not a magic" + " Square");
}
}
輸出
Given matrix is a not a magic Square
在這篇文章中,我們探討了使用Java程式語言檢查矩陣是否為幻方的不同方法。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP