求数组中连续子数组(最少有一个元素)的最大和。

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int curmax=0,imax=-9999;//当前和   最大值
int temp;
for(int i=0;i<n;i++)
{
cin>>temp;
curmax+=temp;     //更新当前和
imax=max(imax,curmax);   //更新最大值
curmax=max(curmax,0);   
}
cout<<imax<<endl;
return 0;
}

posted @ 2019-07-08 22:18  龙龙666666  阅读(512)  评论(2编辑  收藏  举报