求字符序列的所有组合

int g_comb_count=0;
void combination_Core(char *s,int a[],int i)
{
	if(*s=='\0')
	{
		bool flag=false;
		for(int j=0;j<i;j++)
		{
			cout<<setw(5)<<static_cast<char>(a[j]);
			flag=true;
		}
		if(flag)
		{
			g_comb_count++;
			cout<<endl;
		}
	}
	else
	{
		combination_Core(s+1,a,i);
		a[i++]=*s;
		combination_Core(s+1,a,i);
	}
}
void combination(char *s)
{
	g_comb_count=0;
	if(s==NULL)
		throw exception("Invalid input");
	int len=strlen(s);
	int *a=new int[len];
	combination_Core(s,a,0);
	delete[]a;
	cout<<"Count:"<<g_comb_count<<endl;
}

void main()
{ 
	char s[]="abcdef";
	combination(s);
	system("pause");
}

  

posted on 2013-07-25 12:31  dyc0113  阅读(190)  评论(0编辑  收藏  举报

导航