1  16 15 14 13
2  17 24  23 12 
3  18 25  22 11
4  19 20  21 10
5   6   7  8  9
一个这样的二维数组如何打印输出?

解决方案 »

  1.   

    建议发到 J2SE 版去,回答的人会多一些。
      

  2.   

    check this out
    你可以自己改需要打印多少class HelixMatrix {    int[][] myMatrix = new int[20][20];
        static int H = 16;
        static int L = 12;
        static int count = 0; //填充数字记数
        int hh = H; //矩阵实际大小
        int ll = L;
        int stepX = hh; //初始步距
        int stepY = ll - 1;
        int x = 0; //初始坐标
        int y = 0;    void fillFromLeftToRight(int step) {
            //从左向右填写
            for (int i = 0; i < step; i++) {
                if (myMatrix[y][x] == 0) {
                    myMatrix[y][x] = ++count;
                } else {
                    x++;
                    myMatrix[y][x] = ++count;
                }
            }
        }    void fillFromUpToDown(int step) {
            //从上向下填写
            for (int i = 0; i < step; i++) {
                y++;
                myMatrix[y][x] = ++count;
            }
        }    void fillFromRightToLeft(int step) {
            //从右向左填写
            for (int i = 0; i < step; i++) {
                x--;
                myMatrix[y][x] = ++count;
            }
        }    void fillFromDownToUp(int step) {
            //从下向上填写
            for (int i = 0; i < step; i++) {
                y--;
                myMatrix[y][x] = ++count;
            }
        }    public void make() {
            //生成矩阵:从左到右、从上到下、从右到左、
            do {
                //从下到上,每一循环填一圈,很好理解吧。:)
                if (count != H * L) {
                    fillFromLeftToRight(stepX--);
                }
                if (count != H * L) {
                    fillFromUpToDown(stepY--);
                }
                if (count != H * L) {
                    fillFromRightToLeft(stepX--);
                }
                if (count != H * L) {
                    fillFromDownToUp(stepY--);
                }
            } while (count != H * L);
        }    public void print() {
            //打印出来,要注意排版哟!J if语句控制补空格
            for (int i = 0; i < L; i++) {
                for (int j = 0; j < H; j++) {
                    if (myMatrix[i][j] < 10) {
                        System.out.print("   " + myMatrix[i][j]);
                    } else if (myMatrix[i][j] >= 100) {
                        System.out.print(" " + myMatrix[i][j]);
                    } else {
                        System.out.print("  " + myMatrix[i][j]);
                    }
                }
                System.out.println();
            }
        }
    }
    public class PrintHelixMatrix {    public static void main(String[] args) {
            HelixMatrix aa = new HelixMatrix();
            aa.make();
            aa.print();
        }
    }
      

  3.   

    写完了,跟楼上的方法不一样。public static void main(String[] args) {
      final int HEIGHT = 5;
      final int WIDTH = 5;
      int[][] arr = new int[HEIGHT][WIDTH];  int row = 0;  // 控制数组的行
      int col = 0;  // 控制数组的列
      int cycles = 1;  // 当前写入的圈数,初始化为最外层第一圈    
      // 写入数据的方向
      // 1 - down; 2 - right; 3 - up; 4 - left
      int direct = 1;
      
      for (int num = 1; num <= HEIGHT * WIDTH; num++) {
        if (row == HEIGHT - cycles && col == cycles - 1) {
          direct = 2;
        }
        if (row == HEIGHT - cycles && col == WIDTH - cycles) {
          direct = 3;
        }
        if (row == cycles - 1 && col == WIDTH - cycles) {
          direct = 4;
        }
        if (row == cycles - 1 && col == cycles) {
          direct = 1;
          cycles++;  // 当方向又回到 1 时,说明一圈已经走完,继续下一圈
        }
        arr[row][col] = num;
        // 当方向为向下写时,行增加
        row = direct == 1 ? row + 1 : row;
        // 当方向为向上写时,行减少
        row = direct == 3 ? row - 1 : row;
        // 当方向为向右写时,列增加
        col = direct == 2 ? col + 1 : col;
        // 当方向为向左写时,列减少
        col = direct == 4 ? col - 1 : col;
      }  // 输出数组
      for (int[] ar : arr) {
        for (int a : ar) {
          System.out.printf("%3d", a);
        }
        System.out.println();
      }
    }
      

  4.   

    赋值+排版,while的超强运用public class array {
        public static void main (String[] args) {
            final int a=5, b=5;  //定义矩阵
            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 ( x+1<a && arr[x+1][y]==0 ) {
                    x++; s++; arr[x][y]=s;  //down
                }
                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
                }
            }
            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 ("");
            }
        }
    }
      

  5.   

    题目比较有意思。本人初学C语言,花了很长时间,还是编出来了。编程思路可能和别人不一样。
    调试运行正确。#include<stdio.h>
    #include<string.h>
    #include<conio.h>
    #define N 6
    main()
    {
      int str[N][N]={0};
      int i,j,a,b,up,down,left,right;
      clrscr();
      str[0][0]=1;
      for(i=0,j=0;i<N,j<N;)
      {
        if(i-1<0)  up=1;
          else  up=str[i-1][j];
        if(i+1>=N) down=1;
          else  down=str[i+1][j];
        if(j-1<0)  left=1;
          else  left=str[i][j-1];
        if(j+1>=N)  right=1;
          else  right=str[i][j+1];    if(up!=0&&down!=0&&left!=0&&right!=0)  break;
        else
        if((up!=0&&down==0&&left!=0&&right!=0)||(up!=0&&down==0&&left!=0&&right==0))
           {a=i+1; b=j;}
        else
        if((up==0&&down!=0&&left!=0&&right!=0)||(up==0&&down!=0&&left==0&&right!=0))
          {a=i-1; b=j;}
        else
        if((up!=0&&down!=0&&left==0&&right!=0)||(up!=0&&down==0&&left==0&&right!=0))
          {a=i;   b=j-1;}
        else
        if((up!=0&&down!=0&&left!=0&&right==0)||(up==0&&down!=0&&left!=0&&right==0))
          {a=i;   b=j+1;}
        str[a][b]=str[i][j]+1;
        i=a;
        j=b;
      }
      for(i=0;i<N;i++)
        for(j=0;j<N;j++)
         {
           printf("%4d",str[i][j]);
           if(j==N-1)
     printf("\n");
         }
    }
      

  6.   

    以上程序是本人迄今为止编写的最复杂的程序,见笑。没有别人的参考思路,都自己的想法。思路是对于一个数组元素a,考察它前后左右四个相邻的元素特性,从而给它指定路线,下一个元素是b是在它的前、是后还是上、下?如有高手指点学习程序语言,非常感激。本人QQ119554794,Email:[email protected]