1    2   3    4
16  15   14   5
11  12   13   6
10  9    8    7

解决方案 »

  1.   

    System.out.println("1 2 3 4");
    System.out.println("16 15 14 5");
    System.out.println("11 12 13 6");
    System.out.println("10 9 8 7");
      

  2.   

    你自己出的题就没有归律,一下顺时针,一下倒时针.应该是这样才有归律吧.
    1  2  3  4
    12 13 14 5
    11 16 15 6
    10 9  8  7public class Demo {
        public static void main (String[] args) {
            final int a=4, b=4;  //定义矩阵
            int n=1;  //和打印排版有关
            while (n<a*b) n*=10;
            n=n*n/10;
            int[][] arr = new int[a][b];
            int x=0,y=0,s=1;
            arr[x][y] = s;
            while (s<a*b) {  //以下四个循环换下顺序可变成顺时针
             while ( y+1<b && arr[x][y+1]==0 ) {
                    y++; s++; arr[x][y]=s;  //right
                }
                while ( x-1>=0 && arr[x-1][y]==0 ) {
                    x--; s++; arr[x][y]=s;  //up
                }
                while ( y-1>=0 && arr[x][y-1]==0 ) {
                    y--; s++; arr[x][y]=s;  //left
                }
                while ( x+1<a && arr[x+1][y]==0 ) {
                    x++; s++; arr[x][y]=s;  //down
                }
                
                
                
            }
            for (int i = 0; i < a; i++) {
                for (int j = 0; j < b; j++) {
                    int m=arr[i][j];
                    while ( m<n ){       //打印排版有关
                        System.out.print (" "); m*=10;
                    } System.out.print (arr[i][j]);
                } System.out.println ("");
            }
        }
    }