如何在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:通过使用矩阵的静态初始化

在这种方法中,矩阵元素将在程序中初始化。然后根据算法检查矩阵是否为幻方。

示例

Open Compiler
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

Learn Java in-depth with real-world projects through our Java certification course. Enroll and become a certified expert to boost your career.

方法2:通过使用用户定义的方法

在这种方法中,矩阵元素将在程序中初始化。然后通过将矩阵作为参数传递来调用用户定义的方法,并在方法内部根据算法检查矩阵是否为幻方。

示例

Open Compiler
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编程语言检查矩阵是否为幻方的不同方法。

更新于: 2023年3月6日

4K+ 浏览量

开启你的职业生涯

通过完成课程获得认证

开始学习
广告