方法2:
public static void main(String[] args) {
int[][] c = print(4,4);
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c.length; j++) {
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
public static int[][] print(int x, int n){
int[][] a = new int[n][n];
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++) {
if (i==0||j==n-1){
a[i][j]= x*x-n*n+ i+j+1;
}else if(i==n-1||j==0){
a[i][j]=x*x-(n-2)*(n-2)+1-i-j;
}
}
}
if (n<=2){
return a;
}
int[][] a1 = print(x, n-2);
for (int i = 1; i <= n-2; i++) {
for (int j = 1; j <= n-2; j++) {
a[i][j]=a1[i-1][j-1];
}
}
return a;
}
}
|