杭电1003 Max Sum (dp)

上篇博客中存在一点问题,上篇链接

准确的说是oj存爱缺陷吧。具体为:

eg: 如输入为:5 0 0 0 0 0;

  输出为:0 1 5

但是上篇代码已A;正确的输出应为: 0 1 1

题目中已说If there are more than one result, output the first one.


这题可用dp解决,代码为:

#include <stdio.h>
int main()
{
    int i,j,x,n,m,sum,max;
    int start,end,nowstart;
    scanf("%d",&n);
    {
        for (i=1;i<=n;i++)
        {
            scanf("%d%d",&m,&x);
            sum=max=x;
            start=end=nowstart=1;
            for (j=2;j<=m;j++)
            {
                scanf("%d",&x);
                if (sum+x<x)
                {
                    sum=x;
                    nowstart=j;
                }
                else
                {
                    sum+=x;
                    //end=j;
                    /**If there are more than one result,
                    output the first one.!!!!!!!!**/
                }
                if (sum>max)
                {
                    max=sum;
                    start=nowstart;
                    end=j;
                }
            }
            printf("Case %d:\n",i);
            printf("%d %d %d\n",max,start,end);
            if (i!=n)
            {
                printf("\n");
            }
        }
    }
    return 0;
}


你可能感兴趣的:(杭电1003 Max Sum (dp))