Nyoj 983 首尾相连数组的最大子数组和

题目来源: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>

 

 

 

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页