一切皆可抽象

          大而無形 庖丁解牛 厚積薄發(fā) 滌慮玄覽
             ::  ::  ::  ::  :: 管理

          【原創(chuàng)】關(guān)于一個矩陣算法

          Posted on 2005-09-05 14:24 鋒出磨礪 閱讀(1832) 評論(4)  編輯  收藏 所屬分類: java算法

          結(jié)果
          順時針

          01   02   03   04  

          10   11   12   05  

          09   08   07   06  

          逆時針

          04   03   02   01  

          05   12   11   10  

          06   07   08   09  

          代碼
          public class test {
            public test() {
            }
            private static int[][] data = null;

            private int datai = 1;    //數(shù)值
            private static int  h =0, i=0, j;
            private int row1 =0,col1 =0,row2=0,col2=0;
           
            static int x=3,y=4;    // x=? y=?

            public static void main(String[] args)
            {
               j = y-1;
               data = new int[x][y];  //data init  定義了x行 y列的矩陣 用于存放數(shù)據(jù)
               test t = new test();
               t.input(1);           //開始數(shù)據(jù)塞入 1表示 從左到右
               //數(shù)據(jù)輸出
               System.out.println("順時針");
               for (int ki=0;ki     {
                 for (int kj=0;kj       {

                   System.out.print(addZero(String.valueOf(x*y).length(),data[ki][kj])+"   ");
                 }
                 System.out.println("");
               }


               System.out.println("逆時針");
               int[][] kk = niuniu(data,x,y); //矩陣倒置
               for (int ki=0;ki     {
                 for (int kj=0;kj       {

                   System.out.print(addZero(String.valueOf(x*y).length(),kk[ki][kj])+"   ");
                 }
                 System.out.println("");
               }


            }

            private  void input(int typej)
            {
               if (datai > x*y)
               {
                 //System.out.println("exit");如果數(shù)據(jù)塞入到頭 退出遞歸
               }
               else
               {


                //從左到右塞入數(shù)據(jù)
                if (typej == 1) {
                   for (int k = i; k <= j; k++) {
                     data[h][k] = datai++;
                   }

                   row1++;  //上面走了一行
                   h = y - 1 - col1;  //下一步從上到下表示的列值
                   i = row1;          //行起始
                   j = x - 1 - col1;  //行終止
                   input(2);         //從上到下遍歷數(shù)據(jù)
                 }

                 //從上到下塞入數(shù)據(jù)
                 if ( typej == 2) {
                   for (int k = i; k <= j; k++) {
                      data[k][h] = datai++;
                    }

                   col1++;  //左邊走了一列
                   h = x - 1 - row2;  //下一步從右到左表示的行值
                   i = y - 1 - col1;  //列起始
                   j = col2;          //列終止
                   input(3);          //從右到左遍歷數(shù)據(jù)
                 }

                 //從右到左塞入數(shù)據(jù)
                 if ( typej == 3) {
                   for (int k = i; k >= j; k--) {
                     data[h][k] = datai++;
                   }

                   row2++;  //下面走了一行
                   h = col2;  //下一步從下到上表示的列值
                   i = x - 1 - row2;   //行起始
                   j = row1;           //行終止
                   input(4);           //從下到上遍歷數(shù)據(jù)
                 }

                 //從下到上塞入數(shù)據(jù)
                 if (typej == 4) {
                   for (int k = i; k >= j; k--) {
                     data[k][h] = datai++;
                   }

                   col2++;  //左面走了一列
                   h = row1;  //下一步從左到右的行值
                   i = col2;   //列起始
                   j = y - 1 - col1;  //列終止
                   input(1);
                 }
               }
            }

              //補(bǔ)位
              public static String addZero(int weishu, int num) {
              /* int num=new Integer(num).intValue();*/
              int len = Integer.toString(num).length();
              if (len >= weishu) {
                return Integer.toString(num);
              }
              int i = 0;
              int j = weishu - len;
              String BH = "";
              while (i < j) {
                BH = "0" + BH;
                i = i + 1;
              }
              BH = BH + Integer.toString(num);
              return BH;
            }

            //列copy,第1列和最后一列互換 依次類推
            public static int[][] niuniu(int[][] temp,int xi,int yi)
            {
              int[][] rs = new int[xi][yi];
              int foxi = yi/2;
              for (int i=0;i    {
                for (int j=0;j      {
                  rs[j][yi-1-i] = temp[j][i];
                }
              }
              int col = foxi-1;
              int k = 0;
              if (yi%2 == 0)
              {
                k = foxi;
              }
              else
              {
                k = foxi +1;
              }
              for (int i=k;i<=yi-1;i++)
              {
                for (int j=0;j      {
                  rs[j][col] = temp[j][i];
                }
                col--;
              }

              if (yi%2 == 0)
              {
              }
              else
              {
               for (int j=0;j     {
                rs[j][foxi] = temp[j][foxi];
               }

              }

              return rs;
            }


          }


          評論

          # re: 關(guān)于一個矩陣算法  回復(fù)  更多評論   

          2005-09-05 19:46 by Pudgy's World
          這個就是螺旋虧陳矩陣吧。

          # re: 關(guān)于一個矩陣算法  回復(fù)  更多評論   

          2005-09-05 19:49 by 鋒出磨礪
          對 算法比較老土 。如果誰有更好的 請貼出共同參考學(xué)習(xí)。

          # re: 關(guān)于一個矩陣算法  回復(fù)  更多評論   

          2005-09-06 11:36 by ^ Mustang ^
          我們當(dāng)初C語言考試的大題里有一道這樣的

          # re: 【原創(chuàng)】關(guān)于一個矩陣算法  回復(fù)  更多評論   

          2006-05-29 20:19 by 月?lián)?/a>
          class Test5{
          public static void main(String[] args){
          final int count = 10;
          int k = 1;
          int[][] a = new int[count][count];
          for (int n = 0; n<(count+1)/2; n++){ //從外往里需要(count+1)/2圈
          for (int i = n; i<count - n; i++){ //上橫行
          a[i][n] = k++;
          }
          for (int i = n + 1; i<count - n; i++){ //右豎行
          a[count-n-1][i] = k++;
          }
          for (int i = count-n-2; i>=n; i--){ //下橫行
          a[i][count-n-1] = k++;
          }
          for (int i = count-n-2; i>n; i--){ //左豎行
          a[n][i] = k++;
          }
          }
          for (int i = 0; i<count; i++){
          for (int j = 0; j<count; j++)
          System.out.print (a[i][j] + " ");
          System.out.println ();
          }
          }
          }
          我們老師寫的。
          主站蜘蛛池模板: 柳州市| 敦煌市| 辽阳市| 华亭县| 祁东县| 绥中县| 陆良县| 苏尼特右旗| 岑巩县| 鲜城| 昌邑市| 双流县| 平原县| 红原县| 玉林市| 墨脱县| 隆尧县| 右玉县| 宜兰市| 定兴县| 洛南县| 当阳市| 祁东县| 成安县| 资兴市| 辰溪县| 大同市| 比如县| 罗定市| 江山市| 南溪县| 利津县| 东乡族自治县| 河间市| 正阳县| 巴楚县| 南汇区| 兴业县| 澄江县| 瑞丽市| 苗栗市|