@Lin--
2018-09-26T14:41:24.000000Z
字数 978
阅读 271
计算机数学
def product1(x):
res=1
count=0
for i in range(len(x)):
res=res*x[i]
count=count+1
return res,count
#测试
list1=[1,2,3,4,5]
list2=[2]
list3=(range(1000000))
list4=(range(0))
print(product1(list1),product1(list2),product1(list3),product1(list4))
运算过程遍历列表的每一项,故当输入规模为n时,需进行n次运算。
a=0
def product2(x):
global a
if len(x)==0:
a=0
return 1
if len(x)==1:
a=a+1
return x[0]
if len(x)==2:
a=a+2
return x[0]*x[1]
l=len(x)
return product2(x[0:(l+1)//2])*product2(x[(l+1)//2:l])
#测试
list1=[1,2,3,4,5]
list2=[2]
list3=(range(1000000))
list4=(range(0))
print(product2(list1),a)
a=0
print(product2(list2),a)
a=0
print(product2(list3),a)
a=0
print(product2(list4),a)
120 5
2 1
0 1000000
1 0