Java世界

          學習筆記

          常用鏈接

          統計

          積分與排名

          天籟村

          新華網

          雅虎

          最新評論

          一個相關矩陣的打印!

          用Java編寫實現
          打印n階的如下矩陣(矩陣中數字是從中間順時針向外旋轉的)[時間:20分鐘以內]
          n=3
          7 8 9
          6 1 2
          5 4 3
           
          n=4
          7  8  9  10
          6  1  2  11
          5  4  3  12
          16 15 14 13
           
          n=5
          21  22  23  24  25
          20  7   8   9   10
          19  6   1   2   11
          18  5   4   3   12
          17  16  15  14  13

          源代碼如下:

          public class MyMatrix {
               public String[] direct = new String[] { "left", "down", "right", "up" };
               public int n = 5;
               public void print(int[][] matrix, int n) {
                   for (int i = 0; i < n; i++) {
                       for (int j = 0; j < n; j++) {
                           System.out.print(matrix[i][j] + " ");
                       }
                       System.out.println();
                   }
               }
               public void createMatrix() {
                    int seed = 1;
                    int[][] matrix = new int[n][n];
                    int cx = n % 2 == 0 ? n/2 : (n / 2) + 1;
                    int cy = n % 2 == 0 ? n/2 : (n / 2) + 1;

                    int posX = cx - 1;
                    int posY = cy - 1;
                    matrix[posY][posX] = seed;
                    int step=1;
                    int count=1;
                    while(true){
                         //step++;  
                         for (int i = 0; i < direct.length; i++) {
                              String dir = direct[i];    
                
                              for (int j = 0; j < step; j++) {
                                   while (matrix[posY][posX] != 0) {
                                            if (dir == "left") {
                                                 posX++;
                                                 if (posX > n - 1){
                                                      print(matrix, n);
                                                      return;
                                                 }
                                            }
                                            if (dir == "down") {
                                                 posY++;
                                                 if (posY > n - 1){
                                                      print(matrix, n);
                                                      return;
                                                 }
                                            }
                                            if (dir == "right") {
                                                 posX--;
                                                 if (posX < 0){
                                                      print(matrix, n);
                                                      return;
                                                 }       
                                            }
                                            if (dir == "up") {
                                                 posY--;
                                                 if (posY < 0){
                                                      print(matrix, n);
                                                      return;
                                                 }
                                            }
                                       }
                                       matrix[posY][posX] = ++seed;     
                                   }
                                  if(count%2==0){
                                       step++;
                                  }
                                  count++;
                             }
                        }
                   }
               public static void main(String[] args) {
                    MyMatrix matrix = new MyMatrix();
                    matrix.createMatrix();
               }
           
          }

           


          posted on 2007-10-16 13:30 Rabbit 閱讀(270) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 千阳县| 定西市| 尚义县| 鹿邑县| 英德市| 攀枝花市| 金坛市| 宝鸡市| 外汇| 江陵县| 裕民县| 宿松县| 天全县| 呈贡县| 锡林浩特市| 泰州市| 景德镇市| 方城县| 乐平市| 枝江市| 新昌县| 南雄市| 德安县| 丹阳市| 民乐县| 华蓥市| 藁城市| 惠来县| 怀柔区| 贡觉县| 尉氏县| 武强县| 扶沟县| 贞丰县| 图们市| 马山县| 中阳县| 南陵县| 雷波县| 崇信县| 新平|