题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=983
参考:http://blog.csdn.net/hearthougan/article/details/25654383
<strong>#include <iostream>
#include <cstring>
#include <cstdio>
#include <climits>
using namespace std;
int main()
{
int iMax, iMax1, iMax2, sum, tmp, tmp1, iMin, a, n;
while(~scanf("%d", &n))
{
iMax1 = INT_MIN, iMin = INT_MAX, tmp = tmp1 = sum = 0;
while(n--)
{
scanf("%d", &a);
sum += a;
tmp = tmp < 0 ? (tmp + a) : a;//最小连续子段和
tmp1 = tmp1 > 0 ? (tmp1 + a) : a;//最大连续子段和
if(tmp < iMin)
iMin = tmp;
if(tmp1 > iMax1)
iMax1 = tmp1;
}
iMax2 = sum - iMin;
iMax = max(iMax1, iMax2);
if(iMax < 0)
iMax = 0;
printf("%d\n", iMax);
}
return 0;
}
</strong>