Problem1: Magic Square,problem1square
分享于 点击 9302 次 点评:15
Problem1: Magic Square,problem1square
实验环境:
实验要求:
part1:
检验一个矩阵是否为Magic Square:
给出5个文件 1.txt、2.txt、……5.txt,分别检验其是否为Magic Square,结果返回true或false;part2:
public static boolean generateMagicSquare(int n) {
int magic[][] = new int[n][n];
int row = 0, col = n / 2, i, j, square = n * n;
for (i = 1; i <= square; i++) {
magic[row][col] = i;
if (i % n == 0)
row++;
else {
if (row == 0) row = n - 1;
else row--;
if (col == (n - 1)) col = 0;
else col++;
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
System.out.print(magic[i][j] + "\t");
System.out.println();
} return true;
}
实验思路:
part1:
- 用fileInput方法读入文件,并判断上述合法性的2、3点以及是否是一个矩阵(行、列数是否一致)
static boolean fileInput(File file)
- 在isLegalMagicSquare方法中判断矩阵的每一行、每一列、两条对角线加和是否相等
static boolean isLegalMagicSquare(String fileName)
实验收获:
源代码
相关文章
- 暂无相关文章
用户点评