Description
Consider a string A = “12345”. An infinite string s is built by performing infinite steps on A recursively. In i-th step, A is concatenated with ‘…$|reverse(A), where | denotes concatenation.
Constraints:1<=Q<=10^5, 1<=POS<=10^12
Input
输入第一行为查询次数,后面为每次查询的具体字符位置。
Output
输出每一次查询位置上的字符。
Sample Input 1
2
3
10
Sample Output 1
3
2
def solution(number):
# 除了这个迭代A和reverse(A)不一样外,后面的迭代都是一样的
init = ['1', '2', '3', '4', '5', '$', '5', '4', '3', '2', '1']
while number > len(init):
val, iter = location(number)
# 判断number是在中间的$符号,还是在后面的reverse中,temp为上一个A+$的长度
temp = (val - iter) // 2 + iter
if number <= temp:
return "$"
# 说明在reverse中,和上一个迭代A相同
else:
number -= temp
return init[number - 1]
# 求第number个数所在字符串的位置,返回字符串的长度及中间的$个数
def location(number):
val = 5
iter = 0
while number > val:
iter += 1
val = 2 * val + iter
return val, iter
if __name__ == '__main__':
n = int(input())
for i in range(n):
number = int(input())
result = solution(number)
print(result)
来源:CSDN
作者:cy求求你让我过吧
链接:https://blog.csdn.net/qq_26496077/article/details/103240755