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ù)字換回原來位置,準備用第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 小鋒 閱讀(467) 評論(0)  編輯  收藏 所屬分類: algorithm

          主站蜘蛛池模板: 宜兰县| 晋城| 延庆县| 乌鲁木齐县| 离岛区| 琼结县| 临漳县| 高青县| 资源县| 邳州市| 平度市| 驻马店市| 阳朔县| 扶沟县| 临清市| 拜泉县| SHOW| 喀喇| 洪江市| 当阳市| 肥东县| 陇南市| 威海市| 陇西县| 大新县| 阿巴嘎旗| 剑河县| 泰州市| 新津县| 迁安市| 龙岩市| 湘阴县| 商洛市| 鄂托克前旗| 桐梓县| 合作市| 板桥市| 丰宁| 石景山区| 马龙县| 肇庆市|