题目链接:点击打开链接
解题思路:
很经典的一道题。首先考虑一下细节问题,当序列都是0时,显然最后要输出0;当序列都是负数时,显然要输出最大的数。
细节处理完了,就可以回到正常轨道。我们开两个变量,分别保存当前的序列和与之前的最大值,我们更新当前序列和的条件是如果当前序列和是负数的时候,那我们必须更新,否则一定会使最后结果减小。更新过程中还要更新之前最大值即可。
完整代码:
#include <iostream>
#include <cstdio>
#include <climits>
using namespace std;
typedef long long LL;
int n;
const int maxn = 1000001;
LL a[maxn];
LL max(LL a , LL b)
{
return a > b ? a : b;
}
void solve()
{
LL maxpre = 0 , maxnow = 0;
for(int i = 0 ; i < n ; i ++)
{
maxnow = maxnow + a[i];
if(maxnow < 0)
{
maxnow = 0;
}
if(maxnow > maxpre)
maxpre = maxnow;
}
cout << maxpre << endl;
}
int main()
{
#ifdef DoubleQ
freopen("in.txt" , "r" , stdin);
#endif // DoubleQ
while(cin >> n)
{
int flag = 0 , flag2 = 0;
LL maxx = LONG_MIN;
for(int i = 0 ; i < n ; i ++)
{
cin >> a[i];
if(a[i] != 0) flag = 1;
if(a[i] >= 0) flag2 = 1;
maxx = max(maxx , a[i]);
}
if(flag == 0)
{
cout << "0" << endl;
continue;
}
if(flag2 == 0)
{
cout << maxx << endl;
continue;
}
solve();
}
return 0;
}原文:http://blog.csdn.net/u013447865/article/details/45440421