import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int get = in.nextInt();
int[][] gets = new int[get][get];
boolean isEnd = false;
// 方向->右下左上0 1 2 3
// 步数
// 0 1 2 3 0
// 3 2 2 1 1 2*3-1
// 4 3 3 2 2 1 1 2*4-1
// 5 3 3 2 2 1 1 0 0 2*5-1
int direction = 0;
int step = get;
int nowR = 0;
int nowH = 0;
int start = 1;
for (int i = 0; i < get * 2 - 1; i++) {
for (int j = 1; j <= step; j++) {
gets[nowR][nowH] = start;
start++;
if (j != step) {
if (direction == 0) {
nowH += 1; // 右
} else if (direction == 1){
nowR += 1; // 下
} else if (direction == 2) {
nowH -= 1; // 左
} else if (direction == 3) {
nowR -= 1; // 上
}
}
}
if (direction == 3) direction = 0;
else direction += 1;
if (direction == 0) {
nowH += 1; // 右
} else if (direction == 1){
nowR += 1; // 下
} else if (direction == 2) {
nowH -= 1; // 左
} else if (direction == 3) {
nowR -= 1; // 上
}
if (i % 2 == 0) {
step -= 1;
}
}
for (int i = 0; i < get; i++) {
for (int j = 0; j < get; j++) {
System.out.print(gets[i][j] + " ");
}
System.out.println();
}
}
}
没看懂的,欢迎咨询。