public class $ {
public static void main(String[] args) {
int size = 6;
int[] arr = new int[size * size];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
int[][] a = new int[size][size];
test(a, arr[0], size, 0);
show(a);
}
private static void test(int[][] a, int rightUp, int size, int start) {
int end = size - 1 + start;
if (end <= 0) {
return;
}
int leftUp = rightUp + size * 3 - 3;
int leftUpTmp = leftUp;
for (int i = start; i <= end; i++) {
// 第一行
if (i == start) {
for (int j = start; j <= end; j++) {
a[i][j] = leftUpTmp++;
}
a[i][end] = rightUp;
} else if (i == end) {
// 最后一行
for (int j = end; j >= start; j--) {
a[i][j] = ++rightUp;
}
} else {
// 中间行
a[i][start] = --leftUp;
a[i][end] = ++rightUp;
}
}
test(a, a[start][end - 1] + 1, size - 2, start + 1);
}
private static void show(int[][] a) {
for (int[] is : a) {
for (int i : is) {
System.out.print(format(i) + " ");
}
System.out.println();
}
}
private static String format(int num) {
if (num < 10) {
return "0" + num;
}
return "" + num;
}
}
结果:
15 16 17 18 19 00
14 29 30 31 20 01
13 28 35 32 21 02
12 27 34 33 22 03
11 26 25 24 23 04
10 09 08 07 06 05