HDOJ 1003
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 85953 Accepted Submission(s): 19912
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
1 #include <iostream> 2 using namespace std; 3 4 5 int main() 6 { 7 int numbers; 8 cin>>numbers; 9 for(int k =0;k<numbers;k++) 10 { 11 int num; 12 cin>>num; 13 14 15 int * number = new int [num]; 16 int * start = new int [num]; 17 int * b = new int [num]; 18 for(int i=0;i<num;i++) 19 cin>>number[i]; 20 21 b[0]= number[0]; 22 start[0]=0; 23 for(int i=1;i<num;i++) 24 { 25 if(b[i-1]+number[i]>=number[i]) 26 { 27 b[i]=b[i-1]+number[i]; 28 start[i]= start[i-1]; 29 } 30 else 31 { 32 b[i]= number[i]; 33 start[i]= i; 34 } 35 } 36 int max = b[0]; 37 int pos=0; 38 for(int i=0;i<num;i++) 39 { 40 if(b[i]>max) 41 { 42 max=b[i]; 43 pos = i; 44 } 45 } 46 cout<<"Case "<<k+1<<":"<<endl<< max<<" "<<start[pos]+1<<" "<<pos+1<<endl; 47 if(k!=numbers-1) 48 cout<<endl; 49 delete number; 50 delete start; 51 delete b; 52 53 54 } 55 56 57 return 0; 58 59 60 61 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!