您的位置:首页 > 其它

HDOJ 1003 Max Sum

2011-10-07 23:19 351 查看
  一维度的动态规划题目,只需要一个临时变量记录当前最大值即可。起始点与终点的记录算是很巧妙,增加两个变量达到目的。

View Code

#include <stdio.h>

int main()
{
//freopen("Max Sum.txt","r",stdin);
int num;
scanf("%d",&num);
for(int i=1;i<=num;i++)
{
int cnt;
scanf("%d",&cnt);
int end=0,current=0,value,temp=1,start=0,max=-1001;//最小值,Wrong Answer仅仅因为如此
for (int j=1;j<=cnt;j++)
{
scanf("%d",&value);
current+=value;
if(current>=max)
{
max=current;
start=temp;
end=j;
}
if(current<0)
{
current=0;
temp=j+1;
}
}
printf("Case %d:\n%d %d %d\n",i,max,start,end);
if (i!=num)
{
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: