求最大数组之和

def max_child(arr):
max = 0
x = 0
y = 0
n = len(arr)
for i in range(0,n):
for j in range(i,n):
arr_sum = 0
for k in range(i,j+1):
arr_sum = arr_sum + arr[k]
# print(i,j,arr_sum)
if arr_sum > max :
max = arr_sum
x = i
y = j
print("最大子数组的起始-结束下标", x, y)
return max

## 测试用例

arr1 = [1,2,3,4,5]
arr2 = [4,1,3,5,2]
arr3 = [7,6,2,6,4]

print("最大子数组的和:", max_child(arr1))
print("最大子数组的和:", max_child(arr2))
print("最大子数组的和:", max_child(arr3))



运行结果如下:
C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\python.exe D:/pycharm/程序源/最大子数组之和123.py
最大子数组的起始-结束下标 0 4
最大子数组的和: 15
最大子数组的起始-结束下标 0 4
最大子数组的和: 15
最大子数组的起始-结束下标 0 4
最大子数组的和: 25

Process finished with exit code 0

你可能感兴趣的:(求最大数组之和)