【求助】Max Sum

qlyfzz 2011-11-22 09:13:55
题目在 http://acm.hdu.edu.cn/showproblem.php?pid=1003


#include<stdio.h>
#include<stdlib.h>
int a[100001];
int b[100001];
int main(void)
{
int t, n, m, i, j, max, start, end;
scanf("%d", &t);
for(m = 1; m <= t; m ++)
{
scanf("%d", &n);
for(i = 1; i <= n; i ++)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
max = a[1];
start = 1;
end = 1;
for(i = 2; i <= n; i ++)
{
if(b[i - 1] > 0)
b[i] += b[i - 1];
if(b[i] > max)
{
max = b[i];
end = i;
}
}
for(j = end; j > 0; j --)
if(b[j] < 0)
{
start += j;
break;
}
printf("Case %d:\n", m);
printf("%d %d %d\n", max, start, end);
if(m < t)
printf("\n");
}
system("pause");
return 0;
}


为什么在电脑上运行得好像没错啊,可是提交时却说答案错误?

谢谢各位,帮忙看看。
...全文
131 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qlyfzz 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 whizer 的回复:]

最大子串问题,你可以google或者baidu。有多种算法,根据算法去实现,下面给你贴一种 Kadane算法。
C/C++ code
int Kadane(const int array[], size_t length, unsigned int& left, unsigned int& right)
{
unsigned int i, cur_left, ……
[/Quote]


#include<stdio.h>
#include<stdlib.h>
int array[100001];
int main(void)
{
int left, right, i, cur_left, cur_right, t, m, length;
int cur_max, max;
scanf("%d", &t);
for(m = 1; m <= t; m ++)
{
left = right = cur_left = cur_right = 1;
scanf("%d", &length);
scanf("%d", &array[1]);
cur_max = max = array[1];
for(i = 2; i <= length; ++i)
{
scanf("%d", &array[i]);
cur_max += array[i];
if(cur_max >= 0)
{
cur_right = i;

if(max < cur_max)
{
max = cur_max;
left = cur_left;
right = cur_right;
}
}
else
{
cur_max = 0;
cur_left = cur_right = i + 1;
}
}
printf("Case %d:\n", m);
printf("%d %d %d\n", max, left, right);
if(m < t)
printf("\n");
}
system("pause");
return 0;
}
在电脑试好像也没错,可提交还是错了??

#include<stdio.h>
#include<stdlib.h>
int a[100001];
int main(void)
{
int t, n, m, i, j, max, start, end;
scanf("%d", &t);
for(m = 1; m <= t; m ++)
{
scanf("%d", &n);
for(i = 1; i <= n; i ++)
scanf("%d", &a[i]);
max = a[1];
start = 1;
end = 1;
for(i = 2; i <= n; i ++)
{
if(a[i - 1] > 0)
a[i] += a[i - 1];
if(a[i] > max)
{
max = a[i];
end = i;
}
}
if(end != 1)
{

for(j = end; j > 0; j --)
if(a[j] < 0)
{
start += j;
break;
}
}
printf("Case %d:\n", m);
printf("%d %d %d\n", max, start, end);
if(m < t)
printf("\n");
}
system("pause");
return 0;
}

这个改了还是不行。。。
qlyfzz 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qlyfzz 的回复:]

引用 1 楼 woweiwokuang0000 的回复:

数组a有什么用啊?没有处理多解的情况吧?



啊,发现了,没有用。。。

那为什么还是不行啊?测试的几个数据可以啊。
[/Quote]


知道了,4 -1 -1 -1 -1时出错了。
無_1024 2011-11-23
  • 打赏
  • 举报
回复
说句老实话 么看懂题目
qlyfzz 2011-11-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 woweiwokuang0000 的回复:]

数组a有什么用啊?没有处理多解的情况吧?
[/Quote]


啊,发现了,没有用。。。

那为什么还是不行啊?测试的几个数据可以啊。
laciqs 2011-11-23
  • 打赏
  • 举报
回复
暴力求解,枚举起点和终点……
whizer 2011-11-23
  • 打赏
  • 举报
回复
最大子串问题,你可以google或者baidu。有多种算法,根据算法去实现,下面给你贴一种 Kadane算法。
int Kadane(const int array[], size_t length, unsigned int& left, unsigned int& right)  
{
unsigned int i, cur_left, cur_right;
int cur_max, max;

cur_max = max = left = right = cur_left = cur_right = 0;

for(i = 0; i < length; ++i)
{
cur_max += array[i];

if(cur_max > 0)
{
cur_right = i;

if(max < cur_max)
{
max = cur_max;
left = cur_left;
right = cur_right;
}
}
else
{
cur_max = 0;
cur_left = cur_right = i + 1;
}
}

return max;
}
shun_qizi_ran 2011-11-22
  • 打赏
  • 举报
回复
数组a有什么用啊?没有处理多解的情况吧?

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧