1、最大连续子数组和
int MaxSumSubArray(int *a, int length)
{
int currentSum = 0;
int maxSum = a[0];
for(int i = 0; i < length; i++)
{
if(currentSum >= 0)
currentSum += a[i];
else
currentSum = a[i];
if(currentSum > maxSum)
maxSum = currentSum;
}
return maxSum;
}
2、最大连续子数组乘积
double MaxProductSubArray(double *a, int length)
{
double maxEnd = a[0];
double minEnd = a[0];
double maxResult = a[0];
for(int i = 1; i < length; i++)
{
double end1 = maxEnd * a[i];
double end2 = minEnd * a[i];
maxEnd = max(max(end1, end2), a[i]);
minEnd = min(min(end1, end2), a[i]);
maxResult = max(maxResult, maxEnd);
}
return maxResult;
}