@Lin--
2018-09-26T14:41:24.000000Z
字数 978
阅读 366
计算机数学
def product1(x):res=1count=0for i in range(len(x)):res=res*x[i]count=count+1return 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=0def product2(x):global aif len(x)==0:a=0return 1if len(x)==1:a=a+1return x[0]if len(x)==2:a=a+2return 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=0print(product2(list2),a)a=0print(product2(list3),a)a=0print(product2(list4),a)
120 5
2 1
0 1000000
1 0