//最大子数组问题
#include <iostream>
using namespace std;
#define MIN -1024
#define MAX 0x7fffff
//找跨越重点的最大字数组
void FindMaxCrossingSubArray(int *arr, int low, int mid, int high, int &maxLeft, int &maxRight, int &crossSum)
{
int leftSum = MIN;
int sum = 0;
for (int i = mid; i >= low; i--)
{
sum += arr[i];
if (sum > leftSum)
{
leftSum = sum;
maxLeft = i;
}
}
int rightSum = MIN;
sum = 0;
for (int i = mid + 1; i <= high; i++)
{
sum += arr[i];
if (sum > rightSum)
{
rightSum = sum;
maxRight = i;
}
}
crossSum = leftSum + rightSum;
}
void FindMaximumSubArray(int *arr, int low, int high, int &left, int &right, int &sum)
{
if (high == low) //base case:only one element
{
left = low;
right = high;
sum = arr[low];
}
else
{
int mid = (int)((low + high)
最大子数组-分治算法
最新推荐文章于 2023-04-06 18:40:18 发布
本文介绍了一种使用分治算法解决最大子数组问题的方法。通过递归地查找左半部分、右半部分和跨越中间点的最大子数组,最终找到整个数组中的最大子数组。代码示例中展示了如何实现这一算法,并在main函数中进行了测试。
摘要由CSDN通过智能技术生成