杭电acm1003为什么都过不了,显示Runtime Error(ACCESS_VIOLATION)
Givenasequencea[1],a[2],a[3]......a[n],yourjobistocalculatethemaxsumofasub-sequence.F...
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>
#define LEN 5000
int a[LEN];
int main (void)
{
int n,i,j,sum,p,q,max,m,x,y;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
max=a[0];
scanf("%d",&m);
for(j=0;j<m;j++)
scanf("%d",&a[j]);
for(p=0;p<m;p++)
{
sum=0;
for(q=p;q<m;q++)
{
sum+=a[q];
if(sum>max)
{
max=sum;
x=p,y=q;
}
}
}
printf("Case %d:\n%d %d %d\n",i+1,max,x+1,y+1);
printf("\n");
}
}
return 0;
}
我开了很大的数组还是不行的啊?哪里不对? 展开
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>
#define LEN 5000
int a[LEN];
int main (void)
{
int n,i,j,sum,p,q,max,m,x,y;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
max=a[0];
scanf("%d",&m);
for(j=0;j<m;j++)
scanf("%d",&a[j]);
for(p=0;p<m;p++)
{
sum=0;
for(q=p;q<m;q++)
{
sum+=a[q];
if(sum>max)
{
max=sum;
x=p,y=q;
}
}
}
printf("Case %d:\n%d %d %d\n",i+1,max,x+1,y+1);
printf("\n");
}
}
return 0;
}
我开了很大的数组还是不行的啊?哪里不对? 展开
2个回答
展开全部
你的 mm 没有赋初值,所以会宏扰举Runtime Error
还有就是 while的循环结束条件不足,如果到最后 m还是没有==0你就错了
这是ac的蔽碧李派代码
#include
还有就是 while的循环结束条件不足,如果到最后 m还是没有==0你就错了
这是ac的蔽碧李派代码
#include
追问
mm是什么? 我sum不是有初值0了啊?循环结束条件题目没要求 的啊?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询