有个问题请教一下
这样的一道题:给定由n个整数(可能为负整数)组成的序列a1, a2,…, an, 求该序列连续子段和的最大值。当所有整数均为负整数时定义其最大子段和为0。这个程序我是从网上下的,运行结果是正确的,但是该递归函数有个地方无法理解,请高人指点:
#include<iostream>
using namespace std;
int MaxSubSum(int *a, int left, int right)
{ int sum =0;
if (left==right) sum= a[left] >0?a[left]:0;
else { int center = (left+right)/2;
int leftsum = MaxSubSum(a, left, center);
int rightsum = MaxSubSum(a, center+1, right );
int s1 =0; int lefts =0;
for (int i=center; i>=left; i--)
{ lefts += a[i]; if (lefts >s1) s1= lefts; }
int s2 =0; int rights =0;
for (int i=center+1; i<=right; i++)
{ rights += a[i]; if (rights >s2) s2= rights; }
sum = s1+s2;
if (sum < leftsum) sum = leftsum;
if (sum < rightsum) sum = rightsum;
}
return sum; }
int main()
{
int n,a[10000];
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
cout<<MaxSubSum(a,0,n-1);
return 0;
}
递归函数执行到这句就无法理解了int leftsum = MaxSubSum(a, left, center);
int rightsum = MaxSubSum(a, center+1, right ); 这里程序是怎样运行的,怎样才能得到最大子段和的呢?