HDU 1003 MAX SUM

其实用DP思想这个很容易解决,

不过对于DP思想其实也不是很了解,但是算是肤浅的理解了其应用和思想吧。

下面是AC的代码:

#include <iostream>
using namespace std;
const int MAXLIMIT=1000;
int main()
{
	int times;
	cin>>times;
	int arr[MAXLIMIT];
	int j=1;
	while(j<=times)
	{
		int numbers;
		cin>>numbers;
		int max;//tempMax;
		cin>>max;
		int tempMax=max;//读取第一个数
		int pos;//记录当前起点
		int start;//记录最大子序列起点
		int end;//最大子序列终点
		pos=start=end=0;//令当前起点以及终点均为0
		for(int i=1;i<numbers;i++)//因为已经读了第一个数,所以现在开始读的下标应为1,假设这一组数字是在数组里面,存在下标一说
		{
			int temp;
			cin>>temp;//读取这一个数字
			
			if(tempMax+temp<temp)//如果tempMax+temp<temp的时候,说明tempMax<0,这时候,新的子序列开始了
			//之所以不写成tempMax<0用于判断,是因为有可能整个子序列都是负的
			{
				pos=i;//记录当前的位置
				tempMax=temp;//重置tempMax
			}
			else
			{
				tempMax+=temp;//如果tempMax>0,说明前面的子序列还有继续加下去的价值
			}
			if(max<tempMax)//如果max重置
			{
				start=pos;
				end=i;
				max=tempMax;
			}
		}
		cout<<"Case "<<j<<":"<<endl;
		cout<<max<<" "<<start+1<<" "<<end+1<<endl;//从上面看出,start+1才是最大的子序列值
		if(j!=times)
			cout<<endl;
		j++;
			
	}

	return 0;


}









  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值