杭电ACM1003帮看下哪里错了,老是WA,谢谢
代码在这里#include<stdio.h>intmain(){inti,j,m,n,k,l,h,max,a[20],s[20];scanf("%d",&m);h=0;w...
代码在这里
#include <stdio.h>
int main()
{
int i,j,m,n,k,l,h,max,a[20],s[20];
scanf("%d",&m);
h=0;
while(h<m)
{
h++;
scanf("%d",&n);
if(n>=1&&n<=100000)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(j==0)
s[j]=a[j];
else
s[j]=s[j-1]+a[j];
if(s[j]>max)
{
k=i;
l=j;
max=s[j];
}
}
s[i]=0;
}
printf("Case %d:\n",h);
printf("%d %d %d\n\n",max,k+1,l+1);
}
}
}
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 i,j,m,n,k,l,h,max,a[20],s[20];
scanf("%d",&m);
h=0;
while(h<m)
{
h++;
scanf("%d",&n);
if(n>=1&&n<=100000)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(j==0)
s[j]=a[j];
else
s[j]=s[j-1]+a[j];
if(s[j]>max)
{
k=i;
l=j;
max=s[j];
}
}
s[i]=0;
}
printf("Case %d:\n",h);
printf("%d %d %d\n\n",max,k+1,l+1);
}
}
}
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
不好意思,第一次提问... 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询