本帖最后由 何亚辉 于 2016-5-22 18:12 编辑
public abstract class Test8 {
public static void main(String[] args){
int[][] a =print(9, 9);
for (int i =0;i<a.length ;i++ ) {
for (int j =0;j<a.length ;j++ ) {
if (a[j]<10) {
System.out.print(a[j]+" ");
}
else {
System.out.print(a[j]+" ");
}
}
System.out.println();
}
}
public static int[][] print(int x, int n){
int[][] a = new int[n][n];
for (int i =0;i<n ;i++ ) {
for (int j =0;j<n ;j++ ) {
if (i ==0||j==n-1) {
a[j]=i+j+1+x*x-n*n;
} else if (i==n-1||j==0) {
a[j]=x*x-(n-2)*(n-2)-i-j+1;
}
}
}
if (n==1|| n==2) {
return a;
}
int[][] a1 =print(x, n-2);
for (int i =1;i<n-1 ;i++ ) {
for (int j =1;j<n-1 ;j++ ) {
a[j]= a1[i-1][j-1];
}
}
return a;
}
}
|
|