staples 24 没开?# PennySaver - 省钱一族
j*y
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了。
请教一下大家有没有好的解法
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了。
请教一下大家有没有好的解法