# Include <stdio. h>
# Include <stdlib. h>
# Include "random_n.h"
# Define MAX_NUM 100
/*
* Function: the most powerful method to solve the problem
*
* Huge: T = O (N)
*
* Author: Qinzhiguo
*
* Date: 2012-1-30
*/
Int MaxSubSum_HighSpeed (int a [], int N)
{
Int ThisSum, MaxSum, I, j;
ThisSum = MaxSum = 0;
For (I = 0; I <N; I ++)
{
ThisSum + = a [I];
If (ThisSum> MaxSum)
{
MaxSum = ThisSum;
}
Else if (ThisSum <0)
{
ThisSum = 0;
}
}
Return MaxSum;
}
/*
* Function: Max3 is a method to get the biggest one of 3 input numbers
*
* Author: Qinzhiguo
*
* Date: 2012-2-3
*
*/
Int Max3 (int a, int B, int c)
{
If (a> = B & a> = c)
{
Return;
}
Else if (B> = a & B> = c)
{
Return B;
}
Else if (c> = a & c> = B)
{
Return c;
}
Else
{
Return-1;
}
}
/*
* Function: a recursive method to get the sub maxmimu sum of the sequence
*
* Author: Qinzhiguo
*
* Date: 2012-2-3
*/
Int MaxSubQuenceSum (int a [], int left, int right)
{
Int MaxLeftSum, MaxRightSum;
Int MaxLeftBorderSum, MaxRightBorderSum;
Int LeftBorderSum, RightBorderSum;
Int center, I;
If (left = right)
{
If (a [left]> 0)
{
Return a [left];
}
Else
{
Return 0;
}
}
Center = (left + right)/2;
MaxLeftSum = MaxSubQuenceSum (a, left, center );
MaxRightSum = MaxSubQuenceSum (a, center + 1, right );
MaxLeftBorderSum = 0;
LeftBorderSum = 0;
For (I = center; I> = left; I --)
{
LeftBorderSum + = a [I];
If (LeftBorderSum> MaxLeftBorderSum)
{
MaxLeftBorderSum = LeftBorderSum;
}
}
MaxRightBorderSum = 0;
RightBorderSum = 0;
For (I = center + 1; I <= right; I ++)
{
RightBorderSum + = a [I];
If (RightBorderSum> MaxRightBorderSum)
{
MaxRightBorderSum = RightBorderSum;
}
}
Return Max3 (MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum );
}
/*
* Function: get the maxsubsum by giving the argments input.
*
* Author: Qinzhiguo
*
* Date: 2012-2-3
*/
Int MaxSubSum (int a [], int n)
{
Return MaxSubQuenceSum (a, 0, n-1 );
}
/*
* Function: Main indoor of the whole project
*
* Author: Qinzhiguo
*
* Date: 2012-1-31
*/
Int main ()
{
Int a [MAX_NUM];
Int I = 0, MaxSum = 0;
While (I <MAX_NUM)
{
A [I] = 0;
I ++;
}
Random_n (a, MAX_NUM );
MaxSum = MaxSubSum (a, MAX_NUM );
I = 0;
While (I <MAX_NUM)
{
Printf ("% d", a [I]);
I ++;
}
Printf ("\ nThe List MaxSubQueneSum is % d \ n", MaxSum );
If (I = 0)
{
Printf ("\ nNothing is done! \ N ");
}
Else
{
Printf ("\ n ------------ Program Finshed ------------ \ n ");
}
Return 0;
}
The random. h In the header file is a random number generation function written by myself. You can implement it yourself or refer to the method for generating random numbers in my previous blog.
In this way, the test has a relatively large advantage, less the input burden of a large amount of data.
From Mingyue Tianya