全排列的递归实现

来源:互联网 发布:java https请求 编辑:程序博客网 时间:2024/05/11 02:15

 

全排列的递归实现

N个互不相同的元素的全排列一共有N!种,实现N个互不相同的元素的全排列可以用递归的方法来实现。当N=1时,全排列为1;当N=2时全排列为1,22,1。当N=3时,全排列为1,2,3;1,3,2;2,1,3;2,3,1;3,1,2;3,2,13=6种排法

观测发现,当N=1时,全排列就为1;当N>1时,将序列 的首个元素 提出来,将剩下的N-1个元素 进行全排列,然后将首个字母插在 前面和后面即完成一个排列,依此类推,将第二个元素 提取出来,又得到一个全排列直到最后一个元素 进行完成,则整个序列的所有排列方式全部列出。

用递归的方法实现序列的全排列,思想非常简单。但是,运算量是相当大的。当输入26个英文字母的时候,计算时间超过了40分钟。下面附上实现上述算法的C++源代码。

 

/******************************************************

Copyright@2009-2011 by hank(SiChuan University)

*******************************************************/

//perm.h

#ifndef PERM_H

#define PERM_H

const int MaxSize=26;

class array

{

   char* p;

   int s,

      e,

      count;

   void Swap(char& a,char& b);//交换元素  

public:

 

   array();//构造函数

   ~array(){delete []p;}//析构函数

   void perm(char* p,int s,int e);//实现数组元素p[s:e]的全排列

   int getCount()const{return count;}//显示总的排列数

   char* input();//创建数组

 

};

 

#endif

/******************************************************

Copyright@2009-2011 by hank(SiChuan University)

*******************************************************/

//perm.cpp

#include"perm.h"

#include<iostream>

#include<fstream>

using namespace std;

/*********************************************/

//构造函数

array::array()

{

   p=new char[MaxSize];

   s=0;

   e=MaxSize-1;

   count=0;

}

 

/************************************************/

//交换两个元素

void array::Swap(char& a,char& b)

{

   char temp;

   temp=a;

   a=b;

   b=temp;

}

/************************************************/

//实现数组元素p[s:e]的全排列

void array::perm(char* p,int s,int e)

{

   if(s==e)

   {  count++;

      ofstream fout("out.txt",ios_base::app);

      fout<<""<<count<<"种排法为:"<<endl;

      for(int i=0;i<=e;i++)

        fout<<p[i]<<" ";

      fout<<endl;

      fout.close();

  

   }

   else

   {

      for(int j=s;j<=e;j++)

      {

        Swap(p[0],p[j]);

        perm(p,s+1,e);

        Swap(p[0],p[j]);

      }

   }

}

/************************************************/

//创建数组

char* array::input()

{

   int k=MaxSize;

   char in;

   while(k)

   {

      cin>>in;

      p[MaxSize-k]=in;

      k--;

   }

   return p;

}

/******************************************************

Copyright@2009-2011 by hank(SiChuan University)

*******************************************************/

//main.cpp

#include"perm.h"

#include<iostream>

#include<fstream>

using namespace std;

 

int main()

{

   array ar;

   ofstream fout("out.txt",ios_base::app);

   cout<<"************全排列测试开始**********/n"<<endl;

   fout<<"**************全排列测试开始********/n"<<endl;

   cout<<"创建需要排列的元素(输入"<<MaxSize<<"个不相同的字母):"<<endl;

   char*p=ar.input();

   int s=0,e=MaxSize-1;

   ar.perm(p,s,e);

   int count=ar.getCount();

   cout<<"/n************************************"<<endl;

   cout<<"/n总共有"<<count<<"种排法!详细排法参见out.txt    "<<endl;

   fout<<"/n     总共有"<<count<<"种排法!       "<<endl;

   cout<<"/n*********测试结束********"<<endl;

   fout<<"/n*********测试结束**********"<<endl;

   fout.close();

   return 0;

}

 

 

原创粉丝点击