最大子序列的和算法三分析
//算法复杂度T=O(n),
#include<iostream>
using namespace std;
int Maxsum(int*a, int n);
int main()
{
int n, i;
int a[100];
while (cin >> n)
{
for (i = 0; i < n; i++)
cin >> a[i];
cout<<Maxsum(a, n)<<endl;
}
}
int Maxsum(int*a, int n)
{
int i, thissum=0, maxsum = 0;
for (i = 0; i < n; i++){//从开始向右累加
thissum += a[i];
if (thissum>maxsum)
maxsum = thissum; //发现更大的就要更新
else if (thissum < 0) //如果当前子列和为4则不可能是后面增加,于是弃之
thissum = 0;
}
return thissum;
}