这个代码是《数据结构与算法C语言实现》的第2章的练习,代码如下:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#define N 5000
using namespace std;
long int max3(long int a, long int b, long int c)
{
return max(max(a, b), c);
}
// methed three algorithm
long int maxSubSum(const int A[], long int left, long int right)
{
if (left == right)
{
if (A[left] < 0)
return 0;
else
return A[left];
}
// cout<< "sub sequence is "<<endl;
// for(int i = left; i <= right; i++)
// {
// cout<<A[i]<<endl;
// }
long int center = (left + right) / 2;
// cout << "left is " << left << endl;
// cout << "right is " << right <<endl;
// cout << "center is " << center << endl <<endl;
long int maxLeftSum = maxSubSum(A, left, center);
long int maxRightSum = maxSubSum(A, center + 1, right);
long int tempLeftSum = 0; long int leftBorderSum = 0;
for(long int i = center; i >= left; i--)
{
tempLeftSum += A[i];
if (leftBorderSum < tempLeftSum)
{
leftBorderSum = tempLeftSum;
}
}
long int tempRightSum = 0; long int rightBorderSum = 0;
for(long int i = center + 1; i <= right; i++)
{
tempRightSum += A[i];
if (tempRightSum > rightBorderSum)
{
rightBorderSum = tempRightSum;
}
}
return max3(maxLeftSum, maxRightSum, rightBorderSum + leftBorderSum);
}
int main() {
// generate a sequence with 50 integers
int * sequence = (int *)malloc(sizeof(int)*N);
// printf("sequence is ");
for(int i = 0; i < N; i++)
{
*(sequence + i) = (int) (rand() % 100) * pow(-1.0 , i);
// printf("%d ", *(sequence + i));
}
// printf("\n");
long int maxSum = 0;
int startIndex = 0;
int endIndex = 0;
/*
* mothed one
*/
printf("======================= mothed one =========================\n");
// get k sub sequence which sum is maximum
// int * maxSubSequence = (int *)malloc(sizeof(int)*k);
for(int i = 0; i < N; i++)
{
for(int j = i; j < N; j++)
{
long int tempSum = 0;
for(int m = i; m <= j; m++)
{
tempSum += *(sequence + m);
}
if (maxSum < tempSum)
{
maxSum = tempSum;
startIndex = i;
endIndex = j;
}
}
}
// print the sum and the sequence
printf("\n the sum is %d \n", maxSum);
// printf("and the sequence is ");
// for (int i = startIndex; i <= endIndex; i++)
// {
// printf("%d ",*(sequence + i));
// }
// printf("\n");
/*
* mothed tree
*/
printf("======================= mothed three =========================\n");
long int finalSum = maxSubSum(sequence, 0, N - 1);
printf("\n the sum is %d \n" , finalSum);
/*
* mothed two
*/
printf("======================= mothed two =========================\n");
maxSum = 0;
startIndex = 0;
endIndex = 0;
for(int i = 0 ; i < N ; i++)
{
int tempSum = 0;
for(int j = i; j < N; j++ )
{
tempSum += *(sequence + j);
if (tempSum > maxSum)
{
maxSum = tempSum;
startIndex = i;
endIndex = j;
}
}
}
// print the sum and the sequence
printf("\n the sum is %d \n", maxSum);
// printf("and the sequence is ");
// for (int i = startIndex; i <= endIndex; i++)
// {
// printf("%d ",*(sequence + i));
// }
// printf("\n");
printf("================== mothed four ==========================\n");
long int thisSum = 0;
maxSum = 0;
for(int i = 0; i < N; i++)
{
thisSum += *(sequence + i);
if (thisSum > maxSum)
{
maxSum = thisSum;
}
else if( thisSum < 0)
{
thisSum = 0;
}
}
printf("\n the sum is %d \n", maxSum);
}