開始學習python,這是我嘗試過的最大總和子數組。結束「比較中超出最大遞歸深度」。我的算法對我來說似乎沒問題。如果我做錯了任何事,請幫助我。超過最大遞歸深度的比較?
import sys
import math
def maxtuple(lss,rss):
if lss[2] > rss[2]:
return lss
else:
return rss
def crosssubarray(A, start, mid, end):
ls=rs=-sys.maxsize
maxleft=0
maxright=0
sum = 0;
for i in reversed(range(start, mid)):
sum = sum + A[i]
print(i)
if sum > ls:
ls = sum
maxleft = i
sum = 0
for i in range(mid+1, end):
sum = sum+ A[i]
if sum > rs:
rs = sum
maxright = i
return (maxleft, maxright, ls+rs)
def maxsubarray(A,start,end):
if start == end:
return (start,end,A[start])
else:
mid = (start+end)/2
lss = maxsubarray(A, start, mid)
rss = maxsubarray(A, mid+1, end)
css = crosssubarray(A, start, mid, end)
maxsub = maxtuple(lss,rss)
maxall = maxtuple(maxsub, css)
return maxall
A = [13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7]
print(maxsubarray(A,0,15))
適合我(一旦縮進是固定的)。 – 101
你的輸出值是多少? – Sivabushan
你試圖得到什麼輸出?您必須向我們顯示您的預期輸出才能完成此問題。 – abccd