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步 − 声明Boolean变量来检查幻方。
第3步 − 使用循环来找出两个对角线的和。
第4步 − 使用for循环找出行和列的和。
第5步 − 检查幻方。
第6步 − 打印结果。
语法
为了获得一个数组的长度(该数组中元素的数量),数组有一个内置的属性,即 length
下面是其语法 –
array.length
其中,“array”指的是数组的引用。
多种方法
我们提供了不同的方法来解决问题。
- 通过使用矩阵的静态初始化
-
通过使用用户定义的方法
让我们逐个查看程序及其输出。
方法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编程语言探索了不同的方法来检查矩阵是否为魔方阵。