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);
}
}

hdu_1003_Max Sum_201311271630的更多相关文章

  1. hdu_1003_Max Sum

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

随机推荐

  1. PCB javascript实现个税5000计算

    个税调整为5000计划实施是今年10月份.而明年一月份全面实施, 马上快到5000个税实施的日子了,当到了个税实施日子时,必定网络会产生热点,这个时候需要就是蹭热点的时候到来时. 全国网友肯定都会关心 ...

  2. day24 03 多继承

    day24 03 多继承 正常的代码中  单继承==减少了代码的重复 继承表达的是一种 子类是父类的关系 1.简单的多继承关系 A,B,C,D四个类,其中D类继承A,B,C三个父类,因此也叫多继承,子 ...

  3. ACM_出题人这样不好吧

    出题人这样不好吧 Time Limit: 2000/1000ms (Java/Others) Problem Description: 作为编协的第一次月赛,肯定是要有防AK(ALL KILL)的题目 ...

  4. 上传Android代码到gerrit服务器

    1. 配置default.xml default.xml是跟Android代码配套的,可参考google Android源码下的default.xml(.repo/manifests/default. ...

  5. azkaban-executor启动时出现conf/global.properties (No such file or directory)的问题解决(图文详解)

     问题详情 // :: INFO [FlowRunnerManager] [Azkaban] Cleaning recently finished // :: INFO [FlowRunnerMana ...

  6. 兼容浏览器 div固定浏览器窗口底部 浮动div

    css内容: <style type="text/css"> #ken_BB { padding-right:30px; text-align: center; col ...

  7. 本地mongochef连接其他计算机上的数据库认证失败解决方法

    关闭防火墙或者在信任程序列表添加运行目录下的mongod.exe即可

  8. 编写高质量的js之恰当选用if和switch

    switch结构中存在很多限制,存在这些限制的主要目的是提高多重分支结构的执行效率.因此,如果能够使用switch结构,就不要选择if结构. 无论是使用if结构,还是使用switch结构,应该确保下面 ...

  9. CSS——行业动态demo

    1.padding的运用:子div继承父div的宽,子div的padding-left值是不会撑大的. 2.背景图片的运用:不平铺.定位 3.ul本身就是一个盒子,它的高度是li中的字体的默认高度撑起 ...

  10. [文章转载]-Java后端,应该日常翻看的中文技术网站 -江南白衣

    Java后端,应该日常翻看的中文技术网站 1.内容生产者 InfoQ 中文技术第一站,佩服霍老板,真金白银地为中国程序员们生产内容. ImportNew 专门面向Java的内容生产者兼聚合者,偶然也有 ...