输入n,打印出下图:(N=5)1 16 15 14 132 17 24 23 123 18 25 22 11 4 19 20 21 105  6  7  8  9
明天就要,快点!

解决方案 »

  1.   

    class HelixMatrix {
          int[][] myMatrix = new int[20][20];
          static int H = 5;
          static int L = 4;
          static int count = 0;  //填充数字记数
          int hh=H;              //矩阵实际大小
          int ll=L;
          int stepX = hh;        //初始步距
          int stepY = 6-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 prtMatrix() {  
              for (int i=0; i<L; i++) {
                  for (int j=0; j<H; j++)
                      if (myMatrix[i][j] < 5) 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 juZhen {
          public static void main(String[] args) {
              HelixMatrix aa = new HelixMatrix();
              aa.make();
              aa.prtMatrix();
          }}
      

  2.   

    class HelixMatrix {
          int[][] myMatrix = new int[20][20];
          static int H = 5;
          static int L = 4;
          static int count = 0;  //填充数字记数
          int hh=H;              //矩阵实际大小
          int ll=L;
          int stepX = hh;        //初始步距
          int stepY = 6-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 prtMatrix() {  
              for (int i=0; i<L; i++) {
                  for (int j=0; j<H; j++)
                      if (myMatrix[i][j] < 5) System.out.print("   "+myMatrix[i][j]);
                      else if (myMatrix[i][j] >= 25) System.out.print(" "+myMatrix[i][j]);
                      else System.out.print("  "+myMatrix[i][j]);
                  System.out.println();
              }
          };
    }public class juZhen {
          public static void main(String[] args) {
              HelixMatrix aa = new HelixMatrix();
              aa.make();
              aa.prtMatrix();
          }}