j******y 发帖数: 180 | 1 看到别人的一个面试题:
The number 131071 = 2^17 - 1 has two interesting properties:
(1) Its base-17 representation, (19b91)17, is palindromic (reading
the same backward as forward).
(2) Its base-2 representation, (11111111111111111)2, has a run of
at least 17 consecutive 1 bits.
What is the next number with both of these properties?
想了下解法, 从初始值开始每次左移一位加一,然后convert成17为base的数, 把每
一位存到list里去, 然后双指针判断是不是回文数。
悲催的是下一个数貌似很大, 我用unsigned long long也不行,最后给overflow了也
没找到。
然后想那么把17个1存string里去,每次append一个1进去,然后把以2为base的string
来convert成以17为base的string,再判断回文
所以会有一个function: bool isPalindromic( string& base2 )。 卡在这个string之
间的base conversion了。
请教一下大家有没有好的解法 | l******s 发帖数: 3045 | 2 一点想法对于长字符串数字的除法与取模,可以二分法切割后为左侧字符串做除法及取
模以较快缩短整个字符串到long范围内。
reading
【在 j******y 的大作中提到】 : 看到别人的一个面试题: : The number 131071 = 2^17 - 1 has two interesting properties: : (1) Its base-17 representation, (19b91)17, is palindromic (reading : the same backward as forward). : (2) Its base-2 representation, (11111111111111111)2, has a run of : at least 17 consecutive 1 bits. : What is the next number with both of these properties? : 想了下解法, 从初始值开始每次左移一位加一,然后convert成17为base的数, 把每 : 一位存到list里去, 然后双指针判断是不是回文数。 : 悲催的是下一个数貌似很大, 我用unsigned long long也不行,最后给overflow了也
| q***a 发帖数: 26 | 3 读题。
"has a run of at least 17 consecutive 1 bits" doesn't mean all bits are 1s.
reading
【在 j******y 的大作中提到】 : 看到别人的一个面试题: : The number 131071 = 2^17 - 1 has two interesting properties: : (1) Its base-17 representation, (19b91)17, is palindromic (reading : the same backward as forward). : (2) Its base-2 representation, (11111111111111111)2, has a run of : at least 17 consecutive 1 bits. : What is the next number with both of these properties? : 想了下解法, 从初始值开始每次左移一位加一,然后convert成17为base的数, 把每 : 一位存到list里去, 然后双指针判断是不是回文数。 : 悲催的是下一个数貌似很大, 我用unsigned long long也不行,最后给overflow了也
| s**********x 发帖数: 120 | 4 我觉得可以直接对字符串乘2加1来计算呀,然后判断新的字符串是不是对称的。
绕开了溢出的问题。 |
|