求连续子数组的最大和

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int MaxSum(int *a, int n){
    int maxv = -1<<30,value = 0;
    for(int i = 0 ; i < n ; i  ++ ){
        value += a[i];
        maxv=max(maxv,value);
        value=max(0,value);
    }
    return maxv;
}

int main(){
    int a[8]={-2,-2,-3,-10,-4,-7,-1,-5};
    cout<<MaxSum(a,8)<<endl;
    return 0;
}

  

posted @ 2013-04-30 19:17  OpenSoucre  阅读(170)  评论(0编辑  收藏  举报