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 小鋒 閱讀(461) 評論(0)  編輯  收藏 所屬分類: algorithm

          主站蜘蛛池模板: 顺平县| 山东省| 咸丰县| 本溪| 丹巴县| 高唐县| 长阳| 精河县| 白山市| 凌源市| 依兰县| 梁山县| 含山县| 博兴县| 七台河市| 宁国市| 舟山市| 富阳市| 兴国县| 富裕县| 类乌齐县| 蓬溪县| 海阳市| 苗栗县| 龙川县| 台北市| 定日县| 黄浦区| 秭归县| 宜川县| 辽宁省| 白玉县| 鄂尔多斯市| 渭南市| 家居| 北安市| 方正县| 绥棱县| 赣榆县| 锦州市| 读书|