最大连续子列和

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
#define N 20010
int a[N];
int main()
{
    int n;
    while(cin>>n){
        for(int i=0;i<n;i++)
            cin>>a[i];
        int s1=0,s2=0;
        for(int i=0;i<n;i++){
            s1+=a[i];
            if(s1>s2) s2=s1;
            else if(s1<0) s1=0;
        }
        cout<<s2<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/wydxry/p/7469171.html