C语言解决最大连续子序列问题的代码

下面内容是关于C语言解决最大连续子序列问题的内容,应该对码农们也有好处。

6

-2 11 -4 13 -5 -2

10

-10 1 2 3 4 -5 -23 3 7 -21

6

5 -8 3 2 5 0

1

10

3

-1 -5 -2

3

-1 0 -2

0




SampleOutput




20 11 13

10 1 4

10 3 5

10 10 10

0 -1 -2

0 0 0







#include

#include

#include

#include

using namespace std;

int main()

{

    int n;

    while(scanf("%d",&n),n)

    {

        int flag=1;

        int temp[10001],now,max;

        cin>>temp[0];

        now=max=temp[0];

        int begin,end,mid;

        begin=end=mid=0;

        for(int i=1; i

            cin>>temp[i];

        for(int j=0; j

            if(temp[j]>=0)

            {

                flag=0;

                break;

            }

        if(flag==0)

        {

            for(int i=1; i

            {

                if(now+temp[i]

                {

                    now=temp[i];

                    mid=i;

                }

                else now+=temp[i];

                if(max

                {

                    max=now;

                    begin=mid;

                    end=i;

                }

            }

            printf("%d %d %dn",max,temp[begin],temp[end]);

        }

        else printf("0 %d %dn",temp[0],temp[n-1]);

    }

    return 0;

}

你可能感兴趣的:(C语言解决最大连续子序列问题的代码)