我的代码对于找出给定正整数的下一个回文是否有效

本文关键字:下一个 回文 是否 有效 整数 代码 于找出 我的 | 更新日期: 2023-09-27 18:33:57

>问题陈述:对于给定的正数,我必须找出紧接的下一个回文。例如:

For 808, output:818
2133, output:2222

我想知道我的代码是否有效,效率如何?这是解决问题的好方法吗?

逻辑解释:我i设置为数字的最左边位置,j最右边的位置,我基本上是在比较 2 个数字。我总是分配num[j]=num[i],并跟踪数字是否大于原始值、小于或等于。最后,即:j-i==1 or j==i,根据数字的偶数或奇数的位数,我查看数字是否变大,并相应地做出决定。

编辑:该数字最多可以有100,000位..这是问题陈述的一部分,所以我试图避免使用蛮力方法。

int LeftNineIndex = 0, RightNineIndex = 0;
bool NumberLesser = false, NumberGreater = false;
string number = Console.ReadLine();
char[] num = number.ToCharArray();
int i, j, x, y;
for (i = 0, j = num.Length - 1; i <= j; i++, j--)
{
      char m;
      Int32.TryParse(num[i].ToString(),out x);
      Int32.TryParse(num[j].ToString(), out y);
      if (x > y)
      {
           NumberGreater = true;
           NumberLesser = false;
      }
      else if (x < y)
      {
           if (j - i == 1)
           {
                NumberGreater = true;
                NumberLesser = false;
                x = x + 1;
                Char.TryParse(x.ToString(), out m);
                num[i] = m;
           }
           else
           {
                NumberGreater = false;
                NumberLesser = true;
           }             
     }
     if ((j == i && NumberGreater == false) || (j - i == 1 && x == y &&  NumberGreater == false))
     {
           if (x != 9)  // if the number is 9, then i can't add 1 to it
           {
                x = x + 1;
                Char.TryParse(x.ToString(), out m);
                num[i] = m;
           }
           else
           {
                if (num.Length != 1)
                {
                    Int32.TryParse(num[LeftNineIndex].ToString(), out x);
                    Int32.TryParse(num[RightNineIndex].ToString(), out y);
                    x = x + 1;
                    Char.TryParse(x.ToString(), out m);
                    num[LeftNineIndex] = m;
                    num[RightNineIndex] = m;
                }
                else
                {
                    // user has entered just '9', in which case I've hard-coded
                    Console.WriteLine("11");
                }
           }
     }
     num[j] = num[i];
     if (x != 9)  //gives us the index of the number closest to the middle, which is not 9
     {
          LeftNineIndex = i;
          RightNineIndex = j;
     }
}
Console.WriteLine(num);

我的代码对于找出给定正整数的下一个回文是否有效

恒定时间内找到下一个回文相对简单:

  • 将输入分成两半(如果长度为奇数,则前半部分更大)
  • 现在对于下一个回文,有两个候选者:
    1. 保留前半部分,修复后半部分
    2. 将前半部分
    3. 递增 1,并修复后半部分
  • 如果第一个候选项大于输入,请选择第二个候选项,否则选择第二个候选项。

BigInteger类型对于实现这一点很有用:

这种方法在输入长度上具有成本线性,即它在数字大小上是对数的。

public static BigInteger NextPalindrome(BigInteger input)
{
    string firstHalf=input.ToString().Substring(0,(input.ToString().Length+1)/2);
    string incrementedFirstHalf=(BigInteger.Parse(firstHalf)+1).ToString();
    var candidates=new List<string>();
    candidates.Add(firstHalf+new String(firstHalf.Reverse().ToArray()));
    candidates.Add(firstHalf+new String(firstHalf.Reverse().Skip(1).ToArray()));
    candidates.Add(incrementedFirstHalf+new String(incrementedFirstHalf.Reverse().ToArray()));
    candidates.Add(incrementedFirstHalf+new String(incrementedFirstHalf.Reverse().Skip(1).ToArray()));
    candidates.Add("1"+new String('0',input.ToString().Length-1)+"1");
    return candidates.Select(s=>BigInteger.Parse(s))
              .Where(i=>i>input)
              .OrderBy(i=>i)
              .First();
}

通过与本机实现进行比较,测试可处理 100000 以下的所有正整数。

第五种情况很容易被遗漏。如果数字由所有9组成,则增加前半部分会更改长度,并且如果当前长度为奇数(9999 ,...),则需要额外的处理。

我就是这样做的。 似乎有效。

        private int FindNextPaladindrone(int value)
    {
        int result = 0;
        bool found = false;
        while (!found)
        {
            value++;
            found = IsPalindrone(value);
            if (found)
                result = value;
        }
        return result;
    }
    private bool IsPalindrone(int number)
    {
        string numberString = number.ToString();
        int backIndex;
        bool same = true;
        for (int i = 0; i < numberString.Length; i++)
        {
            backIndex = numberString.Length - (i + 1);
            if (i == backIndex || backIndex < i)
                break;
            else
            {
                if (numberString[i] != numberString[backIndex])
                {
                    same = false;
                    break;
                }
            }
        }
        return same;
    }