Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 121261    Accepted Submission(s): 28030

Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
 
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
 
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
 
Sample Input
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
 
Sample Output
Case 1: 14 1 4 Case 2: 7 1 6
 
Author
Ignatius.L
 
 #include <stdio.h>

 int main()
{
int k,T;
scanf("%d",&T);
for(k=;k<=T;k++)
{
int i,j;
int sta,end,n,m;
int t=,max=-;
scanf("%d",&n);
sta=end=j=;
for(i=;i<=n;i++)
{
scanf("%d",&m);
t += m;
if(t>max)
{
max=t;
sta=j;
end=i;
}
if(t<)
{
t=;
j=i+;
}
}
if(k-)
printf("\n");
printf("Case %d:\n",k);
printf("%d %d %d\n",max,sta,end);
}
return ;
}

最大连续子序和

不错的资源:

http://blog.csdn.net/luxiaoxun/article/details/7438315

http://blog.csdn.net/code_pang/article/details/7772200

http://blog.csdn.net/shahdza/article/details/6302823

http://www.cnblogs.com/CCBB/archive/2009/04/25/1443455.html

参考代码:

 #include <stdio.h>
int main()
{int n,T,a,sta,end,max,k,i,p,t; scanf("%d",&T);
for(p=;p<=T;p++) {
scanf("%d",&n);
max=-; //因为一个数a 是-1000~1000的,所以这里相当于变成最小值
t=; //表示 某段连续和
sta=end=k=; // sta最大和的开始,end最大和的结束,k记录每次求和的开始
for(i=;i<=n;i++) {
scanf("%d",&a); t+=a;
if(t>max) { //记录最大连续和的值
max=t;
sta=k;
end=i;
}
if(t<) {
t=;
k=i+;
}
} if(p!=) printf("/n");
printf("Case %d:/n",p);
printf("%d %d %d/n",max,sta,end);
}
}

最新文章

  1. 分享20个最新的免费 UI 设计素材给设计师
  2. bean 接收的参数为Class类型
  3. extern关键字
  4. Observer(观察者)-对象行为型模式
  5. nodejs学习篇 (1)webstorm创建nodejs + express + jade 的web 项目
  6. sendfile传输机制
  7. Redis下载及安装部署
  8. 冲突--ScrollView嵌套ListView冲突问题的最优解决方案
  9. mysql主从同步单个表实验记录
  10. [转贴]Eclipse IDE for c++配置
  11. 使用graphics2D给图片上画字符
  12. seajs常用API整理
  13. 【Linux】部署apache
  14. #include&lt;string.h&gt;
  15. 安装rac遇到的问题总结:
  16. 拆除vs发展c++程序开发过程中产生的.ipch和.sdf文件的方法
  17. 浩哥解析MyBatis源码(一)——执行流程
  18. Hive基础(1)---Hive是什么
  19. 如何设置静态IP
  20. 自定义控件之SegmentControlView

热门文章

  1. CSS3径向渐变实现优惠券波浪造型
  2. 客户端JavaScript&#160;Ajax
  3. 如何看待B站疑似源码泄漏的问题?
  4. js 调用微信浏览器内置方法,启动支付
  5. Sql批量插入方法
  6. iOS 中OpenGL ES 优化 笔记 1
  7. Win7 + VS2015 + Python3.6编译
  8. HDU 5306 吉司机线段树
  9. Mysql(Innodb)如何避免幻读
  10. SQL之T-sql 语句操作数据库