最大子序列-N

#include<iostream>
using namespace std;
int MaxSubSum(int A[], int N)
{
	int ThisSum = 0;
	int MaxSum = 0;
	for(int i = 0; i< N; ++i)
	{
		ThisSum += A[i];
		if(ThisSum > MaxSum)
			MaxSum = ThisSum;
		else if(ThisSum < 0)
			ThisSum = 0;
	}
	return MaxSum;
}
int main()
{
	int A[] = {4, -3, 5, -2, -1, 2, 6, -2};
	cout<<MaxSubSum(A, 8);
}

posted on 2016-03-15 19:32  小二杰  阅读(95)  评论(0编辑  收藏  举报

导航