Java世界

          學習筆記

          導航

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿(11)

          收藏夾

          隨筆檔案

          文章檔案

          閱讀排行榜

          評論排行榜

          常用鏈接

          統計

          積分與排名

          天籟村

          新華網

          雅虎

          最新評論

          一個相關矩陣的打印!

          用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 閱讀(272) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 弥勒县| 察隅县| 福海县| 新密市| 玛纳斯县| 加查县| 军事| 高平市| 利川市| 连城县| 马山县| 昌平区| 会同县| 新丰县| 汨罗市| 右玉县| 平潭县| 辰溪县| 普定县| 叶城县| 阳泉市| 理塘县| 诸暨市| 肃北| 天等县| 湖州市| 吴忠市| 汶上县| 北宁市| 垫江县| 徐水县| 秭归县| 介休市| 仙游县| 毕节市| 平利县| 阿勒泰市| 微博| 北安市| 铁岭县| 怀来县|