HDU 1003 子序列的最大和
Max SumTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 293520 Accepted Submission(s): 69675
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 <stdlib.h>
struct sumtail
{
int sum;
int tail;
};
struct sumtail maxSubSum(int a[],int n)
{
struct sumtail max;
max.sum=0;
int sum;
int i,j,k;
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
sum=0;
for(k=i;k<=j;k++)
{
sum+=a[k];
}
if(sum>max.sum)
{
max.sum = sum;
max.tail=j+1;
}
}
}
return max;
}
void print(int m,int N,struct sumtail max)
{
if(N>1)
{
printf("Case %d:\n",m-N);
printf("%d 1 %d",max.sum,max.tail);
printf("\n\n");
}
else
{
printf("Case %d:\n",m-N);
printf("%d 1 %d",max.sum,max.tail);
printf("\n");
}
}
int main()
{
int N,n;
int i;
struct sumtail maxsum;
scanf("%d",&N);
int m=N;
while(N--)
{
int a[1000];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
maxsum = maxSubSum(a,n);
}
print(m,N,maxsum);
}
return 0;
}
*/
#include <stdio.h>
int main()
{
int N;
int count=1;
scanf("%d",&N);
while(N--){
int n,i;
int a[100002];
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
int max=a[1];
int sum=0;
int l=1,left=1,right=1;
for(i=1;i<=n;i++){
sum=sum+a[i];
if(sum>max){
max=sum;
left=l;
right=i;
}
if(sum<0){
sum=0;
l=i+1;
}
}
printf("Case %d:\n",count);
printf("%d %d %d\n",max,left,right);
if(N)printf("\n");
count++;
}
return 0;
}
第一个程序为什么不能AC?