给定一个数,要求输出比它大的最小的非重复数 如给定2312 则最小为2313 重复定义:相邻两数相等
l 示例
输入:iValue =54
返回: 56
输入:iValue =10
返回: 12
输入:iValue =98
返回: 101
输入:iValue =21099
返回: 21201
程序如下:
int FindMoreThanNum(int Num)
{
int temp = Num;
int length = 0;
while (temp)
{
length++;
temp /= 10;
}
int* pIntArray = new int[length];
temp = Num;
for (int i = length-1; i >= 0; i--)
{
pIntArray[i] = temp%10;
temp /= 10;
}
//找到第一个重复数的位置
int index = 1;
for (; index < length; index++)
{
if (pIntArray[index] == pIntArray[index-1])
{
break;
}
}
if (index == length)//无重复数字
{
index--;
}
while (index >= 0)
{
int j = 0;
//对重复数进行加操作 如果不超过9且和前一位不相同 则满足条件
for (j = pIntArray[index]+1; j <= 9; j++)
{
if ((index >= 1 && j != pIntArray[index-1]) || index == 0)
{
break;
}
}
if (j <= 9)//说明找到合适的替换值了
{
pIntArray[index] = j;
int flag = 0;
for (int i = index + 1; i < length; i++)
{
pIntArray[i] = flag;
flag ^= 0x01;
}
break;
}else//说明调整的值产生了进位 索引值加1 重新判断
{
if (index >= 1)
{
index--;
}else//已经是数的首位了 说明需要进位 判断结束
{
int result = 1;
int flag = 0;
for (int i = 0; i < length; i++)
{
result = result*10 + flag;
flag ^= 0x01;
}
return result;
}
}
}
int result = 0;
for (int i = 0; i < length; i++)
{
result = 10*result + pIntArray[i];
}
return result;
}