hdu 1231 最大连续子序列 ,1003 Max Sum;
2017-11-30 18:37
405 查看
题目(1231)
#include<stdio.h> #include<iostream> using namespace std; int main() { int K,num[10010],cnt; int end,start,thisMax,Max,temp; while(cin>>K&&K) { cnt=0; for( int i=0; i<K; i++) { scanf("%d",&num[i]); if(num[i] < 0 ) cnt++; } if(cnt == K) { printf("%d %d %d\n",0,num[0],num[K-1]); continue; } start = end = 0; Max = thisMax = num[0]; for( int i=1; i<K; i++ ) { if(thisMax < 0) { temp = i; thisMax = 0; } thisMax += num[i]; if(thisMax > Max) { Max = thisMax; end = i; start = temp; } } printf("%d %d %d\n",Max,num[start],num[end]); } return 0; }虽然看了别人的博客AC了但是自己还是不知其所以然。。。
题目(1003)
这道题和上面那道题没有多大的区别,可以说是一模一样,唯一有点疑惑的是,上面那道题的temp 没有赋予初值却对,下面这道题就不可以了#include<stdio.h> #include<cstring> #include<iostream> using namespace std; int main() { int T,cnt=0,n; int num[100010]; int thisMax,Max,temp; scanf("%d",&T); while(T--) { int start, end; memset(num,0,sizeof(num)); cnt++; scanf("%d",&n); for(int i=0; i<n; i++) scanf("%d",&num[i]); start = end = temp = 0; thisMax = Max = num[0]; for( int i=1; i<n; i++ ) { if(thisMax < 0) { temp = i; thisMax = 0; } thisMax += num[i]; if(thisMax > Max) { Max = thisMax; end = i; start = temp; } } if(cnt != 1) printf("\n"); printf("Case %d:\n",cnt); printf("%d %d %d\n",Max,start+1,end+1); } return 0; }
相关文章推荐
- hdu 1003 (Max Sum) hdu 1231(最大连续子序列)
- hdu 1003 Max Sum(分而治之||最大连续子序列 dp)
- HDU 1003 Max Sum 最大连续子序列的和
- hdu 1003 Max Sum(最大连续子序列和) (学了一下分治)
- hdu 1003 Max Sum (最大连续子序列)
- HDU-1003-Max Sum(dp经典问题-最大连续子序列和)
- HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)
- HDU 1003 Max Sum(最大连续子序列和 经典DP)
- HDU 1003.Max Sum【最大连续子序列和】【8月14】
- 【HDU】 1003 Max Sum(最大连续子序列和)
- HDU 1003 Max Sum (入门DP[1] 最大连续子序列)
- hdu 题目1003 Max Sum (最大连续子序列和)
- hdu 1003 Max Sum (最大连续子序列)
- hdu 1231&1003 -最大连续子序列-动态规划
- HDU 1003 Max Sum(最大连续子序列和)
- 六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)
- HDU 1003 Max Sum【最大连续子序列】
- 六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)
- HDU 1003 Max Sum 最大连续子序列和
- 【最大连续子序列和dp】hdu 1003 Max Sum