版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qfzhangwei/article/details/79594300
/**
* Created by on 2018/3/17.
*/
public class JiaZai {
private static final int[][] array;
static{
array= new int[][]{{1, 2, 3,4}, {5,6,7,8},{9,10,11,12},{13,14,15,16}};
}
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
print(4);
}
/**
* [0,0] [0,1] [0,2] [0,3]
* [1,0] [1,1] [1,2] [1,3]
* [2,0] [2,1] [2,2] [2,3]
* [3,0] [3,1] [3,2] [3,3]
*/
//右上角 x ≤ n-1 划分为右上角
// 对角线 x == y 对角线
//左下角 y ≤ n-1 划分为左下角
public static void print(int n) {
int x = 0, y = n - 1;
for (int k = 0; k < n * n; k++) {
System.out.print(array[x][y]+"---");
if (x < y) {
if (y == n - 1) {
y = y - x - 1;
x = 0;
System.out.println();
} else {
x++;
y++;
}
} else if (x == y) {
if (x < n - 1) {
x++;
y++;
} else {
x = 1;
y = 0;
System.out.println();
}
} else {
if (x == n - 1) {
x = x - y + 1;
y = 0;
System.out.println();
} else {
x++;
y++;
}
}
}
}
}
结果如下:
4---
3---8---
2---7---12---
1---6---11---16---
5---10---15---
9---14---
13---