题目:数字以0123456789101112...的格式序列化到一个字符序列中。在这个序列中,第五位是5,第13位是1,第19位是4。求任意第n位对应的数字。
def index_num(n):
if n<10:
return n
i = 0
countDigit = 0
countInteger = 0
while countDigit<n:
i += 1
if i == 1:
countInteger = 10
else:
countInteger = (10**(i-1))*9
countDigit += i * countInteger
countDigit -= i * countInteger
remainDigit = n - countDigit
beginNum = 10**(i-1)
plu_d = remainDigit // i
r_d = remainDigit % i
return str(beginNum+plu_d)[r_d]
print(index_num(1001))
注:
先判断n属于几位整数(1位,2位,3位。。。1位10个,2位90个,3位900个。。。),然后求出n所在哪一个整数,以及属于该整数的第几位。