题目链接:
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 158421 Accepted Submission(s): 37055
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 #include2 #include 3 4 using namespace std; 5 6 struct node 7 { 8 int s,e,smax; 9 } sum[100010];10 11 int main ()12 {13 int t,n,Max,k,j;14 int num[100010];15 while (~scanf("%d",&t))16 {17 int flag=1;18 while (t--)19 {20 21 scanf("%d",&n);22 for (int i=0; i sum[i-1].smax+num[i])33 {34 sum[i].smax=num[i];35 sum[i].s=i;36 sum[i].e=i;37 }38 else39 {40 sum[i].smax=sum[i-1].smax+num[i];41 sum[i].s=sum[i-1].s;42 sum[i].e=i;43 }44 if (Max