HDU 1003 MAX SUM

来源:互联网 发布:识时务,知进退,善其身 编辑:程序博客网 时间:2024/05/18 00:39

其实用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;//令当前起点以及终点均为0for(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;}


0 0
原创粉丝点击