我对前缀和算法有疑问
I have a question in prefix sum algorithm
import time,random
n=int(input("put your number:"))
X=[random.randint(-999,999) for i in range(n)]
print(X)
#pr1 start
before1 = time.clock()
def prefixSum1(X, n): # code for prefixSum1
for i = 0 to n-1 do
S[i] = 0
for j = 0 to i do
S[i]=S[i]+X[j]
return (S)
after1 = time.clock()
#pr2 start
before2 = time.clock()
def prefixSum2(X, n): # code for prefixSum2
S[0]= X[0]
for i = 1 to n-1 do
S[i]=S[i-1]+X[i]
return (S)
after2 = time.clock()
#
random.seed()
print(after1 - before1)
print(after2 - before2)
这是我的代码,我要做的是随机生成从 -999 到 999 的数组 X,并检查两种算法的时间。但是每当我 运行 代码时,它都会显示 >
Makefile:6: recipe for target 'py3_run' failed
make: *** [py3_run] Error 1
File "Main.out", line 11
for i = 0 to n-1 do
^
SyntaxError: invalid syntax
我不知道我的代码有什么问题。我试着在网上找到但他们似乎和我一样...
import time,random
n=int(input("put your number:"))
X=[random.randint(-999,999) for i in range(n)]
print(X)
#pr1 start
before1 = time.clock()
def prefixSum1(X, n): # code for prefixSum1
for i = 0 to n-1 do
S[i] = 0
for j = 0 to i do
S[i]=S[i]+X[j]
return (S)
after1 = time.clock()
#pr2 start
before2 = time.clock()
def prefixSum2(X, n): # code for prefixSum2
S[0]= X[0]
for i = 1 to n-1 do
S[i]=S[i-1]+X[i]
return (S)
after2 = time.clock()
#
random.seed()
print(after1 - before1)
print(after2 - before2)
这是我的代码,我要做的是随机生成从 -999 到 999 的数组 X,并检查两种算法的时间。但是每当我 运行 代码时,它都会显示 >
Makefile:6: recipe for target 'py3_run' failed
make: *** [py3_run] Error 1
File "Main.out", line 11
for i = 0 to n-1 do
^
SyntaxError: invalid syntax
我不知道我的代码有什么问题。我试着在网上找到但他们似乎和我一样...