您的位置:首页 > 编程语言 > C语言/C++

求集合子集,和全排列的递归算法实现(c++,Dev C++调试通过)

2005-11-03 15:24 363 查看
求集合全排列算法实现:

求集合所有子集的算法实现:

1.求集合全排列算法实现:

/*

Name:

Copyright:

Author: XuLei

Date: 01-11-05 09:40

Description:求一个字符串集合(List)的全排列,一共有n!种(假设字符数为n)

Algorithms:令E= {e1 , ..., en }表示n 个元素的集合,我们的目标是生成该集合的所有排列方式。令Ei为E中移去元素i 以后所获得的集合,perm (X) 表示集合X 中元素的排列方式,ei.perm(X)表示在perm (X) 中的每个排列方式的前面均加上ei 以后所得到的排列方式。例如,如果E={a, b, c},那么E1={b, c},perm (E1 )=( b c, c b),e1 .perm(E1) = (a b c, a c b)。对于递归的基本部分,采用n = 1。当只有一个元素时,只可能产生一种排列方式,所以perm
(E) = (e),其中e 是E 中的唯一元素。当n > 1时,perm (E) = e1 .perm(E1) +e2 .perm(E2) +e3.perm(E3) + ... +en .perm (En)。这种递归定义形式是采用n 个perm(X) 来定义perm(E), 其中每个X 包含n-1个元素。至此,一个完整的递归定义所需要的基本部分和递归部分都已完成。

*/

#include <iostream>

using namespace std;

//const int ListLength=10;

const int ListLength=3; //字符串数组的长度

void Swap(char &c, char &s) //交换字符c和s

{

char temp=c;

c=s;

s=temp;

}

void Perm(char *List, int m, int k)

{

static int count=0;

if(m==k)

{

cout<<++count<<":";

for(int i=0; i<=ListLength-1; i++)

{

cout<<List[i];

}

cout<<endl;

}

else

{

for(int i=m; i<=k; i++)

{

Swap(List[m],List[i]);

Perm(List, m+1, k);

Swap(List[m],List[i]);



}

}



}

int main()

{

//char List[ListLength]={'a','b','c','d','e','f','g','h','i','j'};

char List[ListLength]={'a','b','c'};

Perm(List, 0, ListLength-1);

system("pause");

return 0;

}

2. 求集合所有子集的算法实现:

/*

Name:

Copyright:

Author: XuLei

Date: 01-11-05 11:34

Description: 求一个集合(List)的所有子集,并输出

Algorithms: 由SubSet函数来求所有的子集,SubSet(char *List, int m, char *Buffer, int flag)基本思想为首先取出List[m],然后依次把List[m+1...ListLength-1]加到List[m]后面,每加一个,存储在集合Buffer[]中,并输出。由flag标识数组Buffer的长度。以集合{a,b,c}为例,首先取出a存入Buffer[0],输出。然后调用SubSet(char *List, 1, char *Buffer, 1)把Buffer[1]=b输出ab。再调用SubSet(char
*List, 2, char *Buffer, 2) 把Buffer[2]=c输出abc。再进入SubSet(char *List, 1, char *Buffer, 1) 把Buffer[1]=c输出ac。退回最外层的循环。取出b存入Buffer[0],输出。然后调用SubSet(char *List, 1, char *Buffer, 1)把Buffer[1]=c输出bc。取出c存入Buffer[0],输出。

*/

#include <iostream>

using namespace std;

const int ListLength=10;

//const int ListLength=3;

//输出Buffer集合

void Output(char *Buffer, int flag)

{

static int count=1;

if(count==1)

{

cout<<count++<<": { }"<<endl;

}

cout<<count++<<": {";

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

{

cout<<Buffer[i];

}

cout<<"}"<<endl;

}

//找到元素c在集合List中的位置

int Index(char *List, char c)

{

for(int i=0; i<=ListLength-1; i++)

{

if(c==List[i])

{

return i;

break;

}

}

return -1;

}

void SubSet(char *List, int m, char *Buffer, int flag)

{

if(m <= ListLength-1)

{

/*if(m==0)

{

Buffer[0]=List[0];

}*/

//Buffer[flag]=List[m];

/*if(flag==0)

{

Buffer[flag]=List[m];

}*/



for(int i=(flag==0) ? 0 : Index(List,Buffer[flag-1])+1; i<=ListLength-1; i++)

//当flag==0时,Buffer中没有任何元素,此时i=[0...ListLength-1]

//当flag>0时,找到Buffer中的最后一个元素在集合List中的位置i,把[i....ListLength-1]

//处的元素,加到Buffer元素的最后面

{

Buffer[flag]=List[i];

Output(Buffer,flag);

SubSet(List, m+1, Buffer,flag+1);

}

}

return;

}

int main()

{

char List[ListLength]={'a','b','c','d','e','f','g','h','i','j'};

//char List[ListLength]={'a','b','c'};

char Buffer[ListLength]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};

//char Buffer[ListLength]={' ',' ',' '};

//int flag=0;

//TEST

//cout<<Index(List,'c'); OK

SubSet(List,0,Buffer,0);

system("pause");

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: