实现字典序的递增

bool g_Increase=false;
char *Increase(char *s)
{
	g_Increase=false;
	if(s==NULL)
		return NULL;
	int len=strlen(s);
	if(len==1)
		return s;
	int i=len-2;
	while(i>=0&&s[i]>=s[i+1])
		i--;
	if(i<0)
	{
		return s;
	}
	char key=s[i];
	int j;

	for(j=i+1;j<len&&s[j]>key;j++);
	if(j==len)
	{
		swap(s[i],s[j-1]);
		j=len-1;
	}
	else
	{
		j-=1;
		swap(s[i],s[j]);
	}
	i++;
	while(i<j)
	{
		swap(s[i],s[j]);
		i++;
		j--;
	}
	g_Increase=true;
	return s;


}
void main()
{ 
	char s[6]={'1','3','5','7','9','\0'};
	while(true)
	{
		Increase(s);
		if(g_Increase)
		{
		/*	for(int i=0;i<6;i++)
				s[i]=s[i]+48;*/
			cout<<s<<endl;
		}
		else
			break;
	}

	system("pause");
}

  

posted on 2013-07-23 17:09  dyc0113  阅读(348)  评论(0编辑  收藏  举报

导航