Feng.Li's Java See

          抓緊時間,大步向前。
          隨筆 - 95, 文章 - 4, 評論 - 58, 引用 - 0
          數據加載中……

          全排列的遞歸算法

          void perm(char *list, int i, int n){
          int j,temp;
          if(i == n){
          for(j = 0; j < n; j++)
          cout<<list[j];
          cout<<" ";
          }
          else{
          for(j = i; j < n; j++){
          SWAP(list[i],list[j],temp);
          perm(list,i+1,n);
          SWAP(list[i],list[j],temp);
          }

          for(j = i; j < n; j++){
          SWAP(list[i],list[j],temp); //將第j數字做為第一個
          perm(list,i+1,n);
          SWAP(list[i],list[j],temp); //將第j數字換回原來位置,準備用第j+1個做做為第一個

          }
          -----------------------------------------------------------
          建議你將函數修改成以下形式,自己觀察一下
          -----------------------------------------------------------
          void perm(char *list, int i, int n){
          int j,temp;
          for(j = 0; j < n; j++)
          cout<<list[j]<<endl;
          if(i == n){
          for(j = 0; j < n; j++)
          cout<<list[j];
          cout<<" "<<i=n了<<" ";
          }
          else{
          for(j = i; j < n; j++){
          SWAP(list[i],list[j],temp);
          perm(list,i+1,n);
          SWAP(list[i],list[j],temp);

          }
          ========================================================================================================

          下面是完整的程序:C#

          using System;
          using System.Collections.Generic;
          using System.Text;

          namespace Test
          {
              class Program
              {
                  static void Main(string[] args)
                  {
                      char[] list = {'a','b','c','d' };
                      perm(list, 0, 4);
                      Console.ReadLine();
                  }
                  static void perm(char[] list, int i, int n)
                  {
                      int j,temp;
                      if(i==n)
                      {
                       
                          for(j=0;j<n;j++)
                          {
                              Console.Write(list[j]);
                              //Console.WriteLine();
                          }
                      }
                      else

                         
                      {
                         
                          for (j=i;j<n;j++)
                          {
                              swap(ref list[i],ref list[j]);
                              perm(list,i+1,n);
                              swap(ref list[i], ref list[j]);
                          }
                      }
                  }
                  static void swap(ref char  a, ref char  b)
                  {
                      char c = a;
                      a = b;
                      b = c;
                     
                    
                  }
              }
          }


          posted on 2007-10-09 08:58 小鋒 閱讀(458) 評論(0)  編輯  收藏 所屬分類: algorithm

          主站蜘蛛池模板: 新营市| 宁晋县| 常熟市| 葫芦岛市| 临西县| 明水县| 新密市| 洛浦县| 大连市| 凌源市| 扶余县| 铜川市| 安阳市| 乌拉特前旗| 景东| 华容县| 永清县| 定南县| 花莲县| 鄂尔多斯市| 阿克陶县| 峨眉山市| 手机| 永新县| 吉安县| 巍山| 高清| 正镶白旗| 海盐县| 宜都市| 云梦县| 八宿县| 芒康县| 华宁县| 南阳市| 东至县| 石楼县| 南华县| 石首市| 辽中县| 永昌县|