hdu 1231 最大连续子序列 ,1003 Max Sum;
来源:互联网 发布:人际网络网上商城 编辑:程序博客网 时间:2024/06/04 19:27
题目(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;}
阅读全文
0 0
- hdu 1231 最大连续子序列 ,1003 Max Sum;
- HDU 1003 Max Sum(最大连续子序列和)
- hdu 1003 Max Sum (最大连续子序列)
- hdu 1003 MAX SUM(最大连续子序列和)
- HDU 1003 Max Sum 最大连续子序列的和
- HDU 1003 Max Sum(dp,最大连续子序列和)
- HDU 1003 Max Sum(dp,最大连续子序列和)
- hdu 1003 Max Sum (最大连续子序列)
- HDU 1003----Max Sum(最大连续子序列和)
- hdu 1003 Max Sum(分而治之||最大连续子序列 dp)
- HDU 1003 Max Sum 最大连续子序列和
- 【最大连续子序列和dp】hdu 1003 Max Sum
- HDU 1003 Max Sum【最大连续子序列】
- 【HDU】 1003 Max Sum(最大连续子序列和)
- hdu 1003 (Max Sum) hdu 1231(最大连续子序列)
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
- Max Sum(最大连续子序列)
- hdu1003 Max Sum 最大连续子序列
- xiunobbs回帖时间排序修改
- 将文件夹下的多个文件的内容合并到一个文件中
- GCC 关于禁用编译Kernel 是warning as a error
- 广告轮播
- 实验
- hdu 1231 最大连续子序列 ,1003 Max Sum;
- 51Nod 1062 序列中最大的数 打表
- 在实践中深入理解ARP协议
- 模拟@Test功能(注解)
- margin-right负值的使用
- SaltStack 重要配置
- linux跟xftp相连
- mybatis——学习笔记
- 陪陪来了项目苹果端有问题可以留言