一切皆可抽象

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

          結(jié)果
          順時(shí)針

          01   02   03   04  

          10   11   12   05  

          09   08   07   06  

          逆時(shí)針

          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);           //開(kāi)始數(shù)據(jù)塞入 1表示 從左到右
               //數(shù)據(jù)輸出
               System.out.println("順時(shí)針");
               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("逆時(shí)針");
               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列和最后一列互換 依次類(lèi)推
            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;
            }


          }


          評(píng)論

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

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

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

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

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

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

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

          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 ();
          }
          }
          }
          我們老師寫(xiě)的。
          主站蜘蛛池模板: 山东省| 平顶山市| 拉萨市| 长丰县| 石泉县| 平乐县| 潍坊市| 石棉县| 如皋市| 青川县| 教育| 和政县| 新疆| 比如县| 德州市| 玉田县| 越西县| 木兰县| 丰宁| 莱芜市| 新密市| 连州市| 大连市| 巴塘县| 台中县| 额尔古纳市| 阿拉善左旗| 石阡县| 博乐市| 乐平市| 滕州市| 三穗县| 内乡县| 乐安县| 佛教| 疏附县| 仁布县| 始兴县| 滨海县| 山西省| 高碑店市|