如果给一个15位数字最新最好的方式找到下一个回文?回文、最好的、方式、数字

2023-09-11 22:51:57 作者:渡不过的奈何桥╮

在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 ?

推荐答案

分割数为三个部分,中期

1345673 2 9807541

1345673 2 9807541

逆向,并将其与  3765431

Reverse head and compare it to tail 3765431

如果反向(头)LT =尾部(如果他们是平等的初始输入是一个回文,并且希望下一个)

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

如果中期&LT; 9 ,年年递增 否则增量部分,并设置中期:= 0 If mid < 9, increment mid Else increment head part and set mid := 0

结果:= 头反向中期(头)

1345673 3反向(1345673)=> 134567333765431

1345673 3 reverse(1345673) => 134567333765431