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 }
复制代码

 

posted @   xingoo  阅读(532)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示