现在的位置: 首页 > 综合 > 正文

寻找数组中和最大的子序列

2017年11月21日 ⁄ 综合 ⁄ 共 671字 ⁄ 字号 评论关闭

题目连接:http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1001&cid=18563&hide=0
题目陷阱:
1、这道题需要考虑到如果数组中的数都是负数。开始没有考虑到,结果就wrong answer了。
2、AC这个事情,有时候明明觉得自己的代码是对的,就是不能A,好痛苦啊。有时间的时候再好好琢磨吧。先放这里。
参考代码:

#include <stdio.h>
int a[100000];
void main()
{
	int t;
	int c;
	int n;
	int i;
	int sum,pres,pree;
	int temp,s,e;
	scanf("%d",&t);
	c = 1;
	while(t--)
	{
		scanf("%d",&n);
		for(i = 0; i < n; ++i)
		{
			scanf("%d",&a[i]);
		}
		sum = -1000;
		temp = 0;
		s = 0;
		e = 0;
		for(i = 0; i < n; ++i)
		{
			temp += a[i];
			if(temp >= 0)
			{
				e = i;
				if(temp > sum)
				{
					pree = e;
					pres = s;
					sum = temp;
				}
			}
			else
			{
				if(temp > sum)
				{
					pree = e;
					pres = s;
					sum = temp;
				}
				s = i + 1;
				e = i + 1;
				temp = 0;
			}
		}
		printf("Case %d:\n%d %d %d\n\n", c++,sum, pres + 1, pree + 1);
	}
}

 为什么会提示说输出格式错误呢,明明就是对的。
PS:我后来想到了,可能是在输出的最后,我多输入了一个换行符,这样就是错的,可是我怎么样才能保证两个测试样例之间有一个换行呢?

抱歉!评论已关闭.