这种解谜还挺有意思的,这也许就是会员专属的暗号吧
1256. 加密数字
# @param {Integer} num
# @return {String}
def encode(num)
a = (1..30).to_a.map {|it| 2**it}
a.unshift(0)
b = [0]
for i in 1...a.length
b.push(b[-1]+a[i])
end
n = b.bsearch_index {|it| it >= num}
if n == 0
return ""
elsif b[n] == num
return "1"*n
elsif b[n] > num
x = num - b[n-1] - 1
x = x.to_s(2)
if x.length < n
return "0"*(n-x.length) + x
else
return x
end
end
end