求和最大子串

#include<iostream.h>
typedef struct Element
{
    int index;
    int sum;
}Element;
void main()
{
    int array[]={-33,16,-48,5,16,-2,3,-11,10,-1};
    Element elem;
    elem.index=0;
    elem.sum=array[0];
    int temp=array[0];
    for(int i=1;i<sizeof(array)/sizeof(int);i++)
    {
        if(temp<0) temp=0;
        temp+=array[i];
        if(temp>elem.sum)
        {
            elem.index=i;elem.sum=temp;
        }
    }
    cout<<elem.index<<"  "<<elem.sum<<endl;
}

posted on 2012-10-29 19:36  为梦飞翔  阅读(195)  评论(0编辑  收藏  举报

导航