1.蛇形矩阵
public static void printMatrix(int n) {
int[][] array = new int[n][n];
int count = 1;
for (int i = 0; i < 2 * n - 2; i++) {
for (int j = 0; j <= i; j++) {
int x = i - j;
if (x < n && j < n) {
if (i % 2 == 0) {
array[x][j] = count;
} else {
array[j][x] = count;
}
count++;
}
}
}
array[n - 1][n - 1] = count;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
2.回型矩阵