算法与设计第二次作业

```1.伪代码
int maxSum_2(int a[], int low, int high, int &L, int &R)```
`{`
`int mid;`
`int sum = 0;      //记录最大字段`
`int leftsum = 0;  //记录左边最大字段`
`int rightsum = 0; //记录右边最大字段`
```int rs = 0;       //记录中间部分右边字段的最大字段

int ls = 0;       //记录中间部分左边字段的最大字段```
`int lefts = 0;    //记录从中间到最左边的数据之和`
`int rights = 0;   //记录从中间到最右边的数据之和`
`int i;`
`int j;`
`if (low == high)`
`{`
`if (a[low] > 0)`
`sum = a[low];`
`else`
`sum = 0;`
`}`
`if (low < high)`
`{`
`mid = (low + high) / 2;`
`leftsum = maxSum_2(a, low, mid, L, R);       //寻找左边最大字段`
`rightsum = maxSum_2(a, mid + 1, high, L, R); //寻找右边最大字段`
`for (i = mid; i >= low; i--)`
`{`
`lefts += a[i];`
`if (ls < lefts)`
`{`
`ls = lefts;`
`L = i;`
`}`
`}`
`for (j = mid + 1; j <= high; j++)`
`{`
`rights += a[j];`
`if (rs < rights)`
`{`
`rs = rights;`
`R = j;`
`}`
`}`
`sum = ls + rs; //sum保存前后两段的最大字段之和`
`}`
`if (sum < rightsum)`
`{`
`sum = rightsum; //将右边字段设为最大字段`
`L = mid;`
`R = high;`
`}`
`if (sum < leftsum)`
`{`
`sum = leftsum; //将左边字段为最大字段`
`L = low;`
`R = mid;`
`}`
`return sum;`
```}
```

2.时间复杂度

3.体会与思考

...全文
11 回复 打赏 收藏 举报

127

2022-09-26 23:20