输入一个数,要求打出其旋转二维数组,如:
输入3  结果为:  1 8 7
               2 9 6
               3 4 5输入4  结果为:  1 12 11 10
               2 13 16 9
               3 14 15 8
               4  5 6  7

解决方案 »

  1.   

    典型的蛇形矩阵刚学JAVA,还不会使用二维数组,用一维数组模拟的改变 n 就可以生成不同大小的矩阵public class SnakeMatrix {
        public static void main(String[] args){
            
            int layer = 0;          // 圈数, 由外向内
            int i=0, j=0;           // 起始位置
            int direct = 1;         // 初始方向,0,1,2,3---上下左右
            
            int n = 3;
            int a[] = new int[n*n];
            
            for( int num=1; num <= n*n;  num++ )
            {
                a[i*n+j] = num;
                
                // 向 driect 指定的方向移动,遇边转向
                switch( direct ) {
                case 1: // 下
                    if( ++i >= n-1-layer ) {
                        i = n-1-layer;
                        direct = 3; // 转右
                    }
                    break;
                case 3: // 右
                    if( ++j >= n-1-layer ) {
                        j = n-1-layer;
                        direct = 0; // 转上
                    }
                    break;
                case 0: // 上
                    if( --i <= layer ) {
                        i = layer;
                        direct = 2; // 转左
                    }
                    break;
                case 2: // 左
                    if( --j <= layer+1 ) { 
                        j = layer+1;
                        direct = 1; // 转下
                        layer++; // 圈数加1
                    }
                    break;
                default:
                    break;
                }
            }
            
            for(i = 0; i < n; ++i) {
                for(j = 0; j < n; ++j) {
                    System.out.print(a[i*n+j] + "\t");
                }
                System.out.println();
            }
        }
    }
      

  2.   

    刚试出来二维数组的用法int b[][] = new int[n][n];偶开始用 int b[5][5] = new int[5][5] 总是不成
    换用二维数组后,只需要将程序中的 a[i*n+j] 用 b[i][j] 替换即可
      

  3.   

    不用数组写了一个,比较灵活,就是运行起来慢一点。
    public class SnakeMatrix {
        public static void print(long startNo,int r,int c, int n) {
            long result;
            if (c==0) {
                System.out.print(startNo+r + "\t");
            } else if (c==n-1) {
                System.out.print(startNo+3*(n-1)-r + "\t");
            } else if (r==n-1) {
                System.out.print(startNo+r+c + "\t");
            } else if (r==0) {
                System.out.print(startNo+4*(n-1)-c + "\t");
            } else {
                startNo += n*n-(n-2)*(n-2);
                print(startNo,r-1,c-1,n-2);
            }
        }
    public static void main(String[] args) {
        int n = 100;
        long startNo = 1;
        long timeStart = System.currentTimeMillis();
        for (int r=0; r<n; r++) {
            for (int c=0; c<n; c++) {
                print(startNo,r,c,n);
            }
            System.out.println();
        }
        long timeEnd = System.currentTimeMillis();
        System.out.println("Total Time:" + (timeEnd - timeStart));
    }
    }