Java世界

          學習筆記

          導航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          留言簿(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)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 沈阳市| 黄陵县| 河西区| 米泉市| 博野县| 酉阳| 习水县| 响水县| 芒康县| 平山县| 龙南县| 马龙县| 灵台县| 军事| 方山县| 宝应县| 二连浩特市| 潜山县| 汶上县| 巴中市| 溆浦县| 遂川县| 沁阳市| 瑞安市| 滨海县| 平潭县| 连南| 南宁市| 曲阜市| 平定县| 安宁市| 泰州市| 泸水县| 木兰县| 长顺县| 正蓝旗| 勃利县| 宾阳县| 子洲县| 迁安市| 长治县|