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