百度笔试:求大于n的最小的不重复数,不重复数是指相邻两个数不相同

#include <iostream>
using namespace std;

int next_norepeat(int n)
{
	int pow, i, j;
	i=++n;
	pow=0;
	while(i)
	{
		pow++;
		i/=10;
	}

	int *a = new int[pow+1];
	a[0] = 0;
	for(i=pow, j=n; i>0; i--, j/=10)
		a[i] = j%10;

	bool flag = false;
	for(i=1; i<pow; i++)
	{
		if(a[i]==a[i+1])
		{
			a[i+1]++;
			if(a[i+1]==10)
			{
				a[i+1] = 0;
				a[i]++;
				if(a[i]==10)
				{
					a[i] = 0;
					a[i+1] = 1;
					a[i-1]++;
				}
			}
			//i=0;
			flag = true;
			break;
		}
	}
	if(flag)
	{
		for(j=i+2, i=a[i+1]==0?1:0; j<=pow; j++, i=(i+1)&1)
			a[j] = i;

		int next = 0;
		for(i=pow, j=1; i>0; i--, j*=10)
			next += a[i]*j;
		if(a[0]==1)
			next += j;

		return next;
	}
	else
		return n;
}
	

int main()
{
	int next, n;
	while(n!=0)
	{
		cin>>n;
		next = next_norepeat(n);
		cout<<next<<endl;
	}
	return 0;
}

点赞