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. [App Store Connect帮助]二、 添加、编辑和删除用户(3)添加和编辑用户

    您可以在“用户和访问”中添加和编辑用户. 如果您以个人名义在“Apple 开发者计划”注册,您可以授权用户访问您 App Store Connect 中的内容.所有用户只拥有 App Store Co ...

  2. Salvation -- ---广搜 + 限定方向 ,

    这个欣求 , 在迷宫里密室了方向 , 走过了一个地方 不做标记 还一个劲 , 找不到媳妇不亏 . 这个题 我跳了两个坑 ,  1 : 习惯性添加标记走过的 位置  ,导致所有的位置都能 走过一遍 , ...

  3. 【LeetCode】-- 260. Single Number III

    问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, ...

  4. 【转】Java 集合系列02之 Collection架构

    概要 首先,我们对Collection进行说明.下面先看看Collection的一些框架类的关系图: Collection是一个接口,它主要的两个分支是:List 和 Set. List和Set都是接 ...

  5. Android 使用WebView浏览有声音或者视频的网页,关闭WebView之后,声音或者视频不停止的解决办法

    笔者最近使用Eclipse开发Android移动应用app,其实有一个功能是使用Android系统自带的WebView控件加载Web页面.开发很顺利,浏览也很正常.不过有个比较特殊的一点就是加载的We ...

  6. Hibernate中使用子查询

    子查询:   子查询是SQL语句中非常重要的功能特性,它可以在SQL语句中利用另外一条SQL语句的查询结果,在Hibernate中HQL查询同样对子查询功能提供了支持.   如下面代码所示: List ...

  7. [Android]异常5-throwable:java.lang.OutOfMemoryError: pthread_create

    背景:线程初始化耗时任务 异常原因: 可能一>多个new Thread()嵌套 解决办法有: 解决一>使用Handler分离new Thread()嵌套 注: 06-30 09:12:26 ...

  8. Storm 入门一:基本知识+网上资源链接

    Strom首页:http://storm.apache.org/ 下载地址:http://www.apache.org/dyn/closer.lua/storm/apache-storm-1.1.0/ ...

  9. Angular——事件指令

    基本介绍 angular的事件指令都是ng-click,ng-blur....的形式,类似于js的事件 基本使用 <!DOCTYPE html> <html lang="e ...

  10. C# 配置文件ini操作类

    // [ DllImport ( "kernel32" ) ] //private static extern long WritePrivateProfileString ( s ...