import numpy as np
import time
#### 构造题目 , x数组大小
x=100000
l1=np.random.rand(x)
l2=[round(l*x,5) for l in l1]
w1=np.random.randint(0,x)
w2=np.random.randint(0,x)
while w1==w2:
w2=np.random.randint(0,x)
print(‘答案%s,%s‘%(w1,w2))
target=l2[w1]+l2[w2]
print(‘target %s‘%target)
#### 开始计算
t=time.time()
d={}
for i in range(x):
if l2[i] in d.keys():
print(d[l2[i]],i)
print(l2[d[l2[i]]],l2[i])
break
d[target-l2[i]]=i
t=time.time()-t
print(‘%s 元素用时 %s s‘%(x,t))
标签:while 数字 post time() code targe 元素 break 下标
原文地址:https://www.cnblogs.com/offline-ant/p/9309246.html