/*
*奇数魔方阵 :
*将1到n(为奇数)的数字排列在nxn的方阵上,且各行、各列与各对角线的和必须相同
* */
public class Matrix {
public static int[][] magicOdd(int n) {
int[][] square = new int[n + 1][n + 1];
int i = 0;
int j = (n + 1) / 2;
for (int key = 1; key <= n * n; key++) {
if ((key % n) == 1)
i++;
else {
i--;
j++;
}
if (i == 0)
i = n;
if (j > n)
j = 1;
square[j] = key;
}
int[][] matrix = new int[n][n];
for (int k = 0; k < matrix.length; k++) {
for (int l = 0; l < matrix[0].length; l++) {
matrix[k][l] = square[k + 1][l + 1];
}
}
return matrix;
}
public static void main(String[] args) {
int[][] magic = Matrix.magicOdd(5);
for (int k = 0; k < magic.length; k++) {
for (int l = 0; l < magic[0].length; l++) {
System.out.print(magic[k][l] + " ");
}
System.out.println();
}
}
}
运行的结果截图:
运行的结果截图:
|