Max Sumproblem Descriptiongiven A sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a Sub-sequenc E. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Inputthe 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 is b etween-1000 and 1000).
Outputfor Each test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end posi tion of the sub-sequence. If there is more than one result, output the first one. Output a blank line between cases.
Sample Input25 6-1 5 4-77 0 6-1 1-6 7-5
Sample outputcase 1:14 1 4 case 2:7 1 6
#include <cstdlib>
#include <iostream>
using namespace Std;
int main (int argc, char *argv[])
{
int n,m,a,temp,start,end;
Long Sum,max;
cin>>n;
for (int j=1;j<=n;j++)
{
cin>>m;
sum=0;
max=-1001;//// because there are all negative values, assign the maximum value to a negative number (larger also OK)
Temp=1;
for (int i=1;i<=m;i++)
{
cin>>a;
Sum+=a;
if (Sum>max)
{
Max=sum;
start=temp;//Update Start point
End=i; Update end point
}
if (sum<0)
{
sum=0;
temp=i+1;//Execute Next i (that is, read the next number)
}
}
cout<< "Case" <<j<< ":" <<endl;
cout<<max<< "" <<start<< "" <<end<<endl;
if (j<n)
cout<<endl;
}
System ("PAUSE");
return exit_success;
}
Hangzhou Electric 1003-max Sum