PlayXP

새로운 사이트를 준비하는 동안
여러분의 이야기가 이어질 작은 공간을 마련했습니다.

PlayXP 서비스를 준비하며 함께 작업하고 있는 운영진 일러스트

파이썬 코드 recursionerror라는데 한 번만 봐주세용(수정)

조회 수 616

def max_sum(A, left, right):

if left == right-1:

return A[left]

else:

m=(left + start)//2

left_max=max_sum(A,left,med)

right_max=max_sum(A,mid,right)

cross_max=max_cross(A,left,m,right)

if(left_max > right_max > cross_max):

return left_max

elif(right_max > left_max > cross_max):

return right_max

else:

return cross_max

def max_cross(A, left, m, right)

cross_left= float('-inf')

cross_temp= 0

cross_begin=m

for i in range(m -1, left -1, -1):

cross_temp - cross_temp + A

if cross_temp>cross_left:

cross_left = cross_temp

cross_begin = i

cross_right= float('-inf')

cross_temp=0

cross_finish=m+1

for i in range(m, right):

cross_temp = cross_temp + A

if cross_temp>cross_right:

cross_right = cross_temp

cross_finish = i +1

return cross_begin, cross_finish, cross_left+cross_right

# A[left], ..., A[right-1] 중 최대 구간 합 리턴

 

A = input('Enter the list of numbers: ')

A = [int(x) for x in input().split()]

sol = max_sum(A, 0, len(A))

print(sol)​ 

 

분할정복 방법으로 최대구간 합 구하려고 하는데 계속 line 15 그러니까 def max_cross(A, left, m ,right)에서 invalid syntax라고 뜨네요. python 3입니당...

 

def max_sum(A, left, right):

if left == right:

return A[left]

else:

m=(left + right)//2

left_max=max_sum(A,left,m)

right_max=max_sum(A,m,right)

cross_max=max_cross(A,left,m,right)

if(left_max > right_max > cross_max):

return left_max

elif(right_max > left_max > cross_max):

return right_max

else:

return cross_max

def max_cross(A, left, m, right):

cross_left= float('-inf')

cross_temp= 0

cross_begin=m

for i in range(m -1, left -1, -1):

cross_temp - cross_temp + A

if cross_temp>cross_left:

cross_left = cross_temp

cross_begin = i

cross_right= float('-inf')

cross_temp=0

cross_finish=m+1

for i in range(m, right):

cross_temp = cross_temp + A

if cross_temp>cross_right:

cross_right = cross_temp

cross_finish = i +1

return cross_begin, cross_finish, cross_left+cross_right

# A[left], ..., A[right-1] 중 최대 구간 합 리턴

 

A = input('Enter the list of numbers: ')

A = [int(x) for x in input().split()]

sol = max_sum(A, 0, len(A))

print(sol)​ 

 

수정본입니다. 수정했더니 재귀함수 최대횟수 오류가 뜨네용 구글링하니까 파이썬은 천번 밖에 못 돌려서 sys import하고 명령어 넣고 돌리라는데 분명 답지 sample에서는 import sys를 안하고 푼 거 같은데 머가 잘못된건가요

목록으로

댓글

댓글을 불러오는 중...