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 <stdio.h>
int main(int argc, char* argv[])
{
int n, i, j;
scanf("%d", &n);int** a = new int* [n];
for (i = 0; i < n; ++i)
{
a[i] = new int [100000];
}for (i = 0; i < n; ++i)
{
scanf("%d", &a[i][0]);
int k = 0;
while (k++ < a[i][0])
scanf("%d", &a[i][k]);
}int sum, tmp_v, tmp_s, tmp_e, s, e;
for (i = 0; i < n; ++i)
{
printf("Case %d:/n", i+1);tmp_v = 0, sum = 0, tmp_s=tmp_e = 1;
for (j = 1; j <= a[i][0]; ++j)
{
if (tmp_v >= 0)
{
tmp_v += a[i][j];
tmp_e = j;
}
else
{
tmp_v = a[i][j];
tmp_s = j;
}
if (tmp_v > sum)
{
sum = tmp_v;
s = tmp_s;
e = tmp_e;
}
}
if (sum <= 0)
{
sum = a[i][1];
s=e=1;
for(j = 2; j <= a[i][0]; ++j)
if(sum < a[i][j])
{
sum = a[i][j];
s=e=j;
}
}if (i == n-1)
printf("%d %d %d/n", sum, s, e);
else
printf("%d %d %d/n/n", sum, s, e);
}delete [] a;return 0;
}