求一串字符串的全排列和所有组合


//排列函数
void arrangementofstr(char *str,int beg)//str指向待排列的数组,beg为数组的索引位置
{
	if (!str)
	{
		return ;
	}
	if (str[beg]=='')
	{
		cout<<str<<" ";
		return;
	}
	for (int i=beg;str[i]!='';++i)
	{ 
			char tmp=str[beg];
			str[beg]=str[i];
			str[i]=tmp;
		arrangementofstr(str,beg+1);
		    tmp=str[beg];
			str[beg]=str[i];
			str[i]=tmp;
	}
}
//组合函数
void combination(char *pbeg,int n,int j)//pbeg为指向待组合的数组,n为组合数,j为数组索引位置
{
	if (!pbeg)
		return;
	static string tmp;
	if (n==0)
	{
		cout<<tmp<<" ";
		return;
	}
	for (int i=j;i<=strlen(pbeg)-n;++i)//i=j-------------------------!!!!!!!!注意啊啊 啊啊啊  
	{
		tmp.push_back(pbeg[i]);//strlen(pbeg)-n其实不变啦,为窗口大小,窗口向后移动
		combination(pbeg+1,n-1,i);
		tmp.pop_back();
	}
}



void allcombination(char *p)
{
	for (int i=1;i<=strlen(p);++i)
	{
		combination(p,i,0);
		cout<<endl;
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	char a[]="abcde";
	arrangementofstr(a,0);
	//allcombination(a);
	combination1(a,3);
	return 0;
}


运行结果:


原文地址:https://www.cnblogs.com/chhuach2005/p/3961695.html