最大子序列和

老题了。既然又写了遍就记录下

#include <iostream>
#include <cassert>
using namespace std;


int maxSum(int* arr, int length)
{
    assert(arr!=NULL);
    assert(length >=0);
    int sum = 0;
    int max = 0;
    for(int i=0; i<length; i++)
    {
        sum = sum + arr[i];
        if(sum < 0)
            sum = 0;
        if(max < sum)
            max = sum;
    }
    return max;
}


int main(int argc, char* argv[])  
{  
    int array[]={-33,16,-48,5,16,-2,3,-11,10,2};

    cout<<maxSum(array,sizeof(array)/sizeof(array[0]));

    return 0;
}  
posted @ 2012-10-29 19:47  三更_雨  阅读(158)  评论(0编辑  收藏  举报