本帖最后由 kinkon 于 2020-3-2 17:44 编辑
来个不一样的,看看速度如何
1,数字如果不超过 20位 的测这个def p340(n):
a = [0,10,190,2890,38890,488890,5888890,68888890,788888890,
8888888890, 98888888890, 1088888888890,11888888888890,
128888888888890, 1388888888888890,14888888888888890,
158888888888888890, 1688888888888888890, 17888888888888888890]
if n < 10: return n
b = 1
while a[b] < n:
b += 1
n -= a[b - 1]
tmp1, tmp2 = divmod(n, b)
tmp3 = tmp1 + pow(10, b - 1)
return tmp3 // pow(10, b - tmp2 - 1) % 10
2.超过 20位 的测这个def p340(n):
if n < 10: return n
t, a = 1, [0]
for i in range(0, n, pow(10, t)):
if t == 1:
a.append(9*pow(10,t-1)*t + a[t-1] + 1)
else:
a.append(9*pow(10,t-1)*t + a[t-1])
if a[t] >= n:
b = t
break
else:
t += 1
n -= a[b - 1]
tmp1, tmp2 = divmod(n, b)
tmp3 = tmp1 + pow(10, b - 1)
return tmp3 // pow(10, b - tmp2 - 1) % 10
|