Feng.Li's Java See

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

          全排列的遞歸算法

          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數(shù)字做為第一個
          perm(list,i+1,n);
          SWAP(list[i],list[j],temp); //將第j數(shù)字換回原來位置,準(zhǔn)備用第j+1個做做為第一個

          }
          -----------------------------------------------------------
          建議你將函數(shù)修改成以下形式,自己觀察一下
          -----------------------------------------------------------
          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 小鋒 閱讀(459) 評論(0)  編輯  收藏 所屬分類: algorithm

          主站蜘蛛池模板: 社旗县| 美姑县| 常熟市| 呼玛县| 林周县| 轮台县| 莫力| 革吉县| 江口县| 滨州市| 沈阳市| 彩票| 湛江市| 莲花县| 景泰县| 增城市| 青冈县| 凤冈县| 海宁市| 资兴市| 伊通| 林口县| 中江县| 特克斯县| 涡阳县| 蓝山县| 珠海市| 天津市| 阿巴嘎旗| 崇明县| 微山县| 乳山市| 孝感市| 确山县| 济源市| 兰西县| 巴马| 武山县| 合山市| 南陵县| 新绛县|