怎样用Java实现m行n列螺旋数组?最好提炼出方法,谢谢高人!

解决方案 »

  1.   

    你指填充吗? private int[][] array;
    private int m,n;
    public HelixMatrix(int m, int n) {
    this.m = m;
    this.n = n;
    array = new int[m][];
    for(int i = 0; i < m; i++)
    array[i] = new int[n];
    } private void fill() {
    int count = m * n;
    int direct = 0;
    int round = 1;
    for(int index = 1, x = 0, y = 0; index <= count; index++) {
    array[x][y] = index;
    switch(direct) {
    case 0: //向右
    if(y < n - round)
    y++;
    else {
    direct = 1;
    x++;
    }
    break;
    case 1:
    if(x < m - round)
    x++;
    else {
    direct = 2;
    y--;
    }
    break;
    case 2:
    if(y >= round)
    y--;
    else {
    direct = 3;
    x--;
    }
    break;
    case 3:
    if(x > round)
    x--;
    else {
    direct = 0;
    round++;
    y++;
    }
    break;
    }
    }
    } private void printMatrix() {
    for(int i = 0; i < m; i++) {
    for(int j = 0; j < n; j++)
    System.out.printf("%3d,", array[i][j]);
    System.out.println();
    }
    }

    public static void main(String[] args) {
    HelixMatrix matrix = new HelixMatrix(10,6);
    matrix.fill();
    matrix.printMatrix();
    }
      

  2.   

    public class Exetest {
      int[][] con;
    /**
     * 
     */
      public int[][] show(int n,int m){
        con=new int[n][m];
        int count=1;
    for(int i=0;i<n ;i++){
      for(int j=0;j<m;j++){
    if(i%2!=0)
      con[i][m-j-1]=count++;
    else
      con[i][j]=count++;
      }
            }
    print(n,m);
    return con;
      }

      public void print(int n,int m){
        for(int i=0;i<n;i++){
           for(int j=0;j<m;j++)
     System.out.print(con[i][j]+"\t");
           System.out.println();
        }
      }
    }打印结果
    1 2 3 4 5 6 7 8 9 10
    20 19 18 17 16 15 14 13 12 11
    21 22 23 24 25 26 27 28 29 30
    40 39 38 37 36 35 34 33 32 31
    41 42 43 44 45 46 47 48 49 50
    60 59 58 57 56 55 54 53 52 51
    61 62 63 64 65 66 67 68 69 70 LZ不知道是不是想这样啊
      

  3.   

    来个递归的~~public class Test {
    /**
     * @param array
     * 要排序的数组
     * @param num
     * array[0][0]的值,往后递增1
     * @param count
     * 默认为0
     * @return
     */
    public int[][] array(int[][] array, int num, int count) {
    if (count == 0) {
    for (int i = 0; i < array.length; i++)
    for (int j = 0; j < array[0].length; j++)
    array[i][j] = 0;
    }
    if ((count * 2 >= array.length) || (count * 2 >= array[0].length)) {
    if (array.length == array[0].length)
    array[count][count] = num;
    return array;
    }
    for (int i = count; i < array[0].length - 1 - count; i++) {
    array[count][i] = num++;
    }
    for (int i = count; i < array.length - 1 - count; i++) {
    array[i][array[0].length - 1 - count] = num++;
    }
    if (array.length - count * 2 != 1)
    for (int i = array[0].length - 1 - count; i > count; i--) {
    array[array.length - 1 - count][i] = num++;
    }
    else {
    array[array.length - 1 - count][array[0].length - 1 - count] = num;
    }
    if (array[0].length - count * 2 != 1)
    for (int i = array.length - 1 - count; i > count; i--) {
    array[i][count] = num++;
    }
    else {
    array[array.length - 1 - count][count] = num;
    }
    count++;
    return array(array, num, count);
    } public static void main(String[] args) {
    Test ts = new Test();
    int[][] s1 = ts.array(new int[9][9], 1, 0);
    for (int[] ss : s1) {
    for (int s : ss)
    System.out.printf("%3d ", s);
    System.out.println();
    }
    }
    }
      

  4.   

    这样:public class test{
      test(int x,int y){
        for(int i=0;i<x;i++)
          for(int j=0;j<y;j++){
            int u=i>x-i-1?x-i-1:i,v=j>y-j-1?y-j-1:j,layer=u>v?v:u,base=0,a=x-layer*2,b=y-layer*2,addition=j==layer?i-layer+1:i==x-layer-1?j-layer+a:j==y-layer-1?a*2+b-1-(i-layer+1):a*2+b*2-2-(j-layer+1);
            for(int m=(x+y-2)*2,n=layer;m>8&&n>0;m-=8,n--)
              base+=m;
            System.out.print(base+addition+(j==y-1?"\n":" "));
          }
      }
      public static void main(String args[]){
        new test(7,9);
      }
    }
    ========================
    C:\java>java test
    1 28 27 26 25 24 23 22 21
    2 29 48 47 46 45 44 43 20
    3 30 49 60 59 58 57 42 19
    4 31 50 61 62 63 56 41 18
    5 32 51 52 53 54 55 40 17
    6 33 34 35 36 37 38 39 16
    7 8 9 10 11 12 13 14 15