import java.util.Scanner;
public class Test46 {
public static void main(String[] args) {
System.out.println("输入一个数n:");
Scanner mScanner = new Scanner(System.in);
int n = mScanner.nextInt();
mScanner.close();
/* 则返回一个n*n 的矩阵且按逆时针方向排列 */
int[][] arr = new int[n][n];
for (int i = 0, x = -1, y = 0, t = 0; i
int x_2 = x;
int y_2 = y;
if (t % 4 == 0) {
x_2++;
} else if (t % 4 == 1) {
y_2++;
} else if (t % 4 == 2) {
x_2--;
} else if (t % 4 == 3) {
y_2--;
}
if (x_2 >= 0 && x_2 = 0 && y_2
arr[x = x_2][y = y_2] = (i + 1);
i++;
} else {
t++;// 换方向
}
}
/* 打印 */
for (int i = 0; i
for (int j = 0; j
System.out.printf("%-5d", arr[i][j]);
}
System.out.println();
}
}
}