博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Max Sum
阅读量:6758 次
发布时间:2019-06-26

本文共 1886 字,大约阅读时间需要 6 分钟。

题目来源:

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

25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5

Sample Output

Case 1:14 1 4Case 2:7 1 6
#include 
#include
#define MAXLEN 100001const int MIN = -1000;int num[MAXLEN];int main(){ int caseN; while (scanf("%d", &caseN) != EOF) { int cas = 0; while (caseN--) { int n; int tmp = 1; int s = 0, e = 0; int sumMax = MIN; num[0] = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &num[i]); num[0] += num[i]; if (num[0] > sumMax) { sumMax = num[0]; s = tmp; e = i; } if (num[0] < 0 ) { num[0] = 0; tmp = i + 1; } } cas++; printf("Case %d:\n",cas); printf("%d %d %d\n", sumMax,s,e); if (caseN) printf("\n"); } } return 0;}

 

转载地址:http://opweo.baihongyu.com/

你可能感兴趣的文章
进阶第二课 Python内置函数(补)及自定义函数
查看>>
Spell It Right
查看>>
Spring AOP术语解释
查看>>
(一)通过JAVA连接SAP (sapjco3.jar在Windows和MacOS上的配置)
查看>>
《王者荣耀》的英雄是怎么诞生的?有没有最厉害的英雄?
查看>>
公司常用几种请求
查看>>
python3 字符串格式化
查看>>
一个字符在字符串中出现最多的次数的打印
查看>>
图片的三级缓存
查看>>
js跨域问题解决方案
查看>>
(八)统一配置中心-Config
查看>>
I.MX6 Android CAN 命令行测试
查看>>
linux shell except tcl login ssh Automatic interaction
查看>>
iOS JSONModel解析数据成Model
查看>>
Hibernate的映射
查看>>
QQ空间抢车位刷钱方法汇总
查看>>
Quartz2D总结
查看>>
解决数据库报唯一性约束错误的实践
查看>>
Jmeter 4.0 对返回Json处理
查看>>
如何阅读大型代码库?
查看>>