杭电1003哪错了

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"
int main()
{
int i,j,k,n,m,a[200],b[200][200],p,q;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&m);
for(j=1;j<=m;j++)
{
scanf("%d",&a[j]);

}
for(k=1;k<=199;k++)
for(j=1;j<=199;j++)
b[k][j]=0;

for(k=1;k<=m;k++)
{
for(j=1;j<=k;j++)
{

for(q=j;q<=k;q++)
{
b[j][k]+=a[q];

}

}

}
p=1;q=1;
for(k=1;k<=m;k++)
{
for(j=1;j<=k;j++)
{
if(b[j][k]>b[p][q])
{
p=j;q=k;

}

}
}
printf("Case %d:\n",i);

printf("%d %d %d\n",b[p][q],p,q);
if(i!=n)
printf("\n");

}

}

return 0;
}

哪有错
展开
 我来答
Mis_丫
2012-12-25 · TA获得超过373个赞
知道小有建树答主
回答量:272
采纳率:100%
帮助的人:227万
展开全部
#include <stdio.h>

int main()
{
int t,n,i,sum,star,th,end,j,p,k;

scanf("%d",&t);
for (k=1;k<=t;k++)
{
sum = -1000;
star = end = th = j = p = 0;
scanf("%d",&n);
for (j = 0; j < n; j ++)
{
scanf("%d",&i);
th += i;
if (th > sum)
{
sum = th;
star = p;
end = j;
}
if (th < 0)
{
p = j + 1;
th = 0;
}
}
printf("Case %d:\n%d %d %d\n",k,sum,star+1,end+1);
if(k!=t)
printf("\n");

}

return 0;
}
给你看看我的参考下吧,好久没做ACM了,这东西太费脑
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式