在程序设计过程中,对于问题的简化确实很重要。针对最大子序列求和的问题,确实存在很多种方法,而每种方法产生的复杂度就各不相同了。
程序01
/*
Author: Yuan Yuan
Date: 2014-06-15
*/
#include <stdio.h>
#include <stdlib.h>
int MaxSubSeqSum(const int A[], int N)
{
int ThisSum, MaxSum, i, j, k;
MaxSum = 0;
for (i = 0; i < N; i++)
{
for (j = i; j < N; j++)
{
ThisSum = 0;
for (k = i; k <= j; k++)
ThisSum += A[k];
if (ThisSum > MaxSum)
MaxSum = ThisSum;
}
}
return MaxSum;
}
int main(int argc, char *argv[])
{
int arr[10] = {8, -9, 6, 23, -10, 36, -2, 8, 36, -45};
int temp;
//调用函数处理
temp = MaxSubSeqSum(arr, 10);
printf("最大子序列和: %d\n", temp);
return 0;
}
上述程序主要运用的是for循环,分析之后时间复杂度是O(N^3),当子序列太长的时候,时间的增长是呈三次方的,这样会造成过分地耗时。
程序02
/*
Author: Yuan Yuan
Date: 2014-06-15
*/
#include <stdio.h>
#include <stdlib.h>
int MaxSubSeqSum(const int A[], int N)
{
int ThisSum, MaxSum, i, j, k;
MaxSum = 0;
for (i = 0; i < N; i++)
{
ThisSum = 0;
for (j = i; j < N; j++)
{
ThisSum += A[j];
if (ThisSum > MaxSum)
MaxSum = ThisSum;
}
}
return MaxSum;
}
int main(int argc, char *argv[])
{
int arr[10] = {8, -9, 6, 23, -10, 36, -2, 8, 36, -45};
int temp;
//调用函数处理
temp = MaxSubSeqSum(arr, 10);
printf("最大子序列和: %d\n", temp);
return 0;
}
作了小小的变动之后,但是这样的变动时间复杂度就变成了O(N^2),这是相对前面所用的递归来说,改善了很多。
程序03 (分治思想)
/*
Author: Yuan Yuan
Date: 2014-06-15
*/
#include <stdio.h>
#include <stdlib.h>
static int Max3(int num1, int num2, int num3)
{
if (num1 > num2)
{
if (num1 > num3)
return num1;
else return num3;
}
else
{
if (num2 > num3)
return num2;
else return num3;
}
}
static int MaxSubSum(const int arr[], int Left, int Right)
{
int MaxLeftSum, MaxRightSum;
int MaxLeftBorderSum, MaxRightBorderSum;
int LeftBorderSum, RightBorderSum;
int Center, i;
if (Left == Right)
{
if (arr[Left] > 0)
return arr[Left];
else return 0;
}
Center = (Left + Right) / 2;
MaxLeftSum = MaxSubSum(arr, Left, Center);
MaxRightSum = MaxSubSum(arr, Center + 1, Right);
MaxLeftBorderSum = 0;
LeftBorderSum = 0;
for (i = Center; i >= Left; i--)
{
LeftBorderSum += arr[i];
if (LeftBorderSum > MaxLeftBorderSum)
MaxLeftBorderSum = LeftBorderSum;
}
MaxRightBorderSum = 0;
RightBorderSum = 0;
for (i = Center + 1; i <= Right; i++)
{
RightBorderSum += arr[i];
if (RightBorderSum > MaxRightBorderSum)
MaxRightBorderSum = RightBorderSum;
}
return Max3(MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum);
}
int main(int argc, char *argv[])
{
int arr[10] = {8, -9, 6, 23, -10, 36, -2, 8, 36, -45};
int temp;
//调用函数处理
temp = MaxSubSum(arr, 0, 9);
printf("最大子序列和: %d\n", temp);
return 0;
}
采用分治策略,复杂度变成了O(NlogN),相对前面两个算法,这个算法是最好的。
程序04
/*
Author: Yuan Yuan
Date: 2014-06-15
*/
#include <stdio.h>
#include <stdlib.h>
static int MaxSubSeqSum(const int arr[], int N)
{
int ThisSum, MaxSum, i;
ThisSum = 0;
MaxSum = 0;
for (i = 0; i < N; i++)
{
ThisSum += arr[i];
if (ThisSum > MaxSum)
MaxSum = ThisSum;
else if (ThisSum < 0)
ThisSum = 0;
}
return MaxSum;
}
int main(int argc, char *argv[])
{
int arr[10] = {8, -9, 6, 23, -10, 36, -2, 8, 36, -45};
int temp;
//调用函数处理
temp = MaxSubSeqSum(arr, 10);
printf("最大子序列和: %d\n", temp);
return 0;
}
该程序和前面的比较起来,复杂度是O(N),程序执行起来效率更高。只需要一次读入数据就给出问题的正确答案。具有这种特性的算法叫做联机算法,这样的算法几乎是完美的。