最大子段和
发布日期:2021-08-26 19:01:54 浏览次数:5 分类:技术文章

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

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 120365    Accepted Submission(s): 27835

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

 

//动规思想#include
#define MIN -999999using namespace std;int main(){ int T,i,j,sum,n,cnt,start,end,a[100001],ans; cin>>T; cnt=0; while(cnt
>n; cnt++; ans = MIN; sum=0; j=1;//临时起始下标; for(i=1;i<=n;i++) { cin>>a[i]; sum+=a[i]; if(sum>ans){ans=sum;start=j;end=i;} if(sum<0){sum=0;j=i+1;} } cout<<"Case "<
<<":"<

  

转载于:https://www.cnblogs.com/littlehoom/p/3427291.html

转载地址:https://blog.csdn.net/weixin_33894992/article/details/94285822 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:搭建docker私有仓库nexus
下一篇:remote staspack

发表评论

最新留言

不错!
[***.144.177.141]2025年02月05日 08时24分48秒