按照老师讲的第四种算法实现的,时间复杂度为n。
第二种方法应该会超时……
分治下次学习之后再写。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int n,a[100005],i,Max,temp;
while(~scanf("%d",&n))
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
Max=temp=0;
for(i=0;i<n;i++)
{
temp+=a[i];
Max=max(Max,temp);
if(temp<0) temp=0;
}
printf("%d
",Max);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/