如果给定一个 15 位数字,找到下一个回文的最佳方法是什么?

发布于 2024-08-06 09:02:07 字数 71 浏览 3 评论 0原文

在 C++ 中,找到给定 15 位数字的下一个回文的最快逻辑是什么?例如: 134567329807541 的下一个回文是什么?

in c++ what will be the fastest logic to find next palindrome of a given 15 digit number? for example what will be the next palindrome of: 134567329807541 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧故 2024-08-13 09:02:07
  • 将数字分成三部分,headmidtail

    1345673 2 9807541

  • 反向head 并将其与 tail 进行比较
    3765431

  • 如果 reverse(head) <= tail (如果它们相等,则初始输入是回文,并且您需要下一个)

    • 如果中< 9,增加mid
    • 否则增加 head 部分并设置 mid := 0
  • result := head mid reverse(head).

    1345673 3 反向(1345673) => 134567333765431

  • Split the number into three parts, head, mid, tail

    1345673 2 9807541

  • Reverse head and compare it to tail
    3765431

  • If reverse(head) <= tail ( if they are equal the initial input is a palindrome, and you want the next )

    • If mid < 9, increment mid
    • Else increment head part and set mid := 0
  • result := head mid reverse(head).

    1345673 3 reverse(1345673) => 134567333765431

贪了杯 2024-08-13 09:02:07

我相信是这样的

  1. 将数字分成三部分 1345673 2 9807541
  2. 翻转最后一个 1457089
  3. 如果它比第一部分大(在这种情况下)
    • 第一部分++
    • 中间部分 = 0
  4. 翻转第一部分并替换最后一部分。

I believe it's like this

  1. Split the number into three parts 1345673 2 9807541
  2. Flip the last one 1457089
  3. If it's larger than the first part (it is in this case)
    • firstpart++
    • middlepart = 0
  4. flip first part and replace last part.
唔猫 2024-08-13 09:02:07

我不打算实现任何东西,但我想逻辑是:

  1. 在字符串中间分割数字:X 是左侧部分,Y 是右侧部分。
  2. 如果反向(X) <,则令 X' = { X + 1是;否则 X }
  3. 结果是 concat(X',reverse(X'));

如果长度不均匀,则需要将中间的数字分开处理。但这是微不足道的。

I am not about to implement anything, but I imagine the logic would be:

  1. Split the number at the middle of the string: X being the left part and Y being the right part.
  2. Let X' = { X + 1 if reverse(X) < Y; X otherwise }
  3. The result is then concat(X',reverse(X'));

If the length is uneven, you need to treat the middle digit separately. But that is quite trivial.

你是我的挚爱i 2024-08-13 09:02:07

我认为以下算法也应该有效..
实现起来也更容易

  i) Divide the given nos into three parts  HEAD MID TAIL 
  ii) Add 1 to number HEAD MID
          (in case of carry, follow basic addition rules)   
  iii) reverse the new HEAD(store it in HEAD_REV)
  iv) required ans is:-  'new HEAD' MID  HEAD_REV

希望以下示例将有助于更好地理解算法

let nos be:- 23469 9 12367

       So HEAD -> 23469   MID -> 9   TAIL --> 12367

       step 2:-   23469 9 +1 = 23470 0 
              (now HEAD -> 23470 MID -> 0 HEAD_REV -> 07432 )

required Ans:-
23470 0 07432

如果此过程存在任何缺陷,请告诉我

I think the following algo should also work ..
It is easier to implement also

  i) Divide the given nos into three parts  HEAD MID TAIL 
  ii) Add 1 to number HEAD MID
          (in case of carry, follow basic addition rules)   
  iii) reverse the new HEAD(store it in HEAD_REV)
  iv) required ans is:-  'new HEAD' MID  HEAD_REV

Hoping that following example will help in better understanding of the algo

let nos be:- 23469 9 12367

       So HEAD -> 23469   MID -> 9   TAIL --> 12367

       step 2:-   23469 9 +1 = 23470 0 
              (now HEAD -> 23470 MID -> 0 HEAD_REV -> 07432 )

required Ans:-
23470 0 07432

Plz do infrom me if there exist any flaw in this procedure

涫野音 2024-08-13 09:02:07
Split the number into three parts head, mid and tail

if reverse(head)>tail 
result := head mid reverse(head)
else if reverse(head)= tail && mid<9 
    mid++
    result := head mid tail
else 
mid =0
head++
result := head mid reverse(head)
Split the number into three parts head, mid and tail

if reverse(head)>tail 
result := head mid reverse(head)
else if reverse(head)= tail && mid<9 
    mid++
    result := head mid tail
else 
mid =0
head++
result := head mid reverse(head)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文