很是奇怪,为了统一比较,我对每个人的程序都是用time函数来测定用时的,所以把你的程序中datetime部分删除掉了,如果把测时起点放在import numpy之前,用时就长的多,还是0.1s,如果放在numpy之后就只有0.008s了。import time
tt = time.time()
#思路是从高位到低位依次确定数字
import numpy as np
#目前还剩余的数字0~9
shengyushu = list(range(10))
#计算所有可能的阶乘1!,2!,...,10!
jishu = np.array(range(1,11))
ld = np.cumprod(jishu)
weizhi = 1000000#位置
#由高到低依次确定各位上的数字
jieguo = []
lower = 0
for i in range(9,-1,-1):#9,8,7,6,5,4,3,2,1,0
if i==0:
break
for j in range(1,i+1):
if (( lower + j * ld[i-1] <= weizhi ) and (lower + (j+1) * ld[i-1] > weizhi)):
if lower + j*ld[i-1] < weizhi:
jieguo.append(shengyushu.pop(j))
lower = lower + j * ld[i-1]
else:
jieguo.append(shengyushu.pop(j-1))
shengyushu.sort(reverse=True)
jieguo.extend(shengyushu)
break
print(jieguo)
print(time.time() - tt)
[2, 7, 8, 3, 9, 1, 5, 4, 6, 0]
0.1021735668182373
#思路是从高位到低位依次确定数字
import numpy as np
#目前还剩余的数字0~9
import time
tt = time.time()
shengyushu = list(range(10))
#计算所有可能的阶乘1!,2!,...,10!
jishu = np.array(range(1,11))
ld = np.cumprod(jishu)
weizhi = 1000000#位置
#由高到低依次确定各位上的数字
jieguo = []
lower = 0
for i in range(9,-1,-1):#9,8,7,6,5,4,3,2,1,0
if i==0:
break
for j in range(1,i+1):
if (( lower + j * ld[i-1] <= weizhi ) and (lower + (j+1) * ld[i-1] > weizhi)):
if lower + j*ld[i-1] < weizhi:
jieguo.append(shengyushu.pop(j))
lower = lower + j * ld[i-1]
else:
jieguo.append(shengyushu.pop(j-1))
shengyushu.sort(reverse=True)
jieguo.extend(shengyushu)
break
print(jieguo)
print(time.time() - tt)
[2, 7, 8, 3, 9, 1, 5, 4, 6, 0]
0.008064031600952148
难道调用numpy需要很长的时间? |