现在的位置: 首页 > 综合 > 正文

螺旋方阵

2013年09月12日 ⁄ 综合 ⁄ 共 3119字 ⁄ 字号 评论关闭

备份一下老帖子拉,:)
---------------------------------------------------------------------------

从ChinaJavaWorld上看的一道螺旋方阵的题,及我作的结果,大家指点一下。

//这是我的第一个java程序,大家指点一下拉,先谢谢拉。
/*
螺旋矩阵问题
螺旋矩阵问题:编程产生H*L的螺旋矩阵。(为了方便编程我们规定H和L不大于9)。
H=5  L=5                 H=6  L=3           H=2  L=2
1   2   3   4   5        1   2   3   4   5  6        1  2
16  17  18  19  6        14  15  16  17  18  7        4  3
15  24  25  20  7        13  12  11  10  9   8
14  23  22  21  8
13  12  11  10  9
*/

/* ------------------------ 我的一组结果 ----------------------
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16
52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  17
51  96  97  98  99 100 101 102 103 104 105 106 107 108  67  18
50  95 132 133 134 135 136 137 138 139 140 141 142 109  68  19
49  94 131 160 161 162 163 164 165 166 167 168 143 110  69  20
48  93 130 159 180 181 182 183 184 185 186 169 144 111  70  21
47  92 129 158 179 192 191 190 189 188 187 170 145 112  71  22
46  91 128 157 178 177 176 175 174 173 172 171 146 113  72  23
45  90 127 156 155 154 153 152 151 150 149 148 147 114  73  24
44  89 126 125 124 123 122 121 120 119 118 117 116 115  74  25
43  88  87  86  85  84  83  82  81  80  79  78  77  76  75  26
42  41  40  39  38  37  36  35  34  33  32  31  30  29  28  27
--------------------------------------------------------------*/

//: PrintHelixMatrix.java
/*
* CopyRight (C) FreeDebug
* 2003-11-21
* Thank u for reading at my code. ^_^
*/
class HelixMatrix {
      int[][] myMatrix = new int[20][20];
      static int H = 10;
      static int L = 9;
      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              if (myMatrix[y][x]==0) { myMatrix[y][x] = ++count; }
              else { x++; myMatrix[y][x] = ++count; }              
          }          
      };
      void fillFromUpToDown(int step) {
          for (int i=0; i              y++;              
              myMatrix[y][x] = ++count;
          }
      };
      void fillFromRightToLeft(int step) {
          for (int i=0; i              x--;
              myMatrix[y][x] = ++count;
          }
      };
      void fillFromDownToUp(int step) {
          for (int i=0; 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              for (int j=0; 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.prtMatrix();
      }

}
//:~

举人 乾坤一笑 发表于 2003-11-21 16:36:29

抱歉!评论已关闭.