最大连续子序列和问题

问题描述:给定一个序列a[1],a[2]...a[n],求解其连续子序列中元素和的最大值
例如: 6 -1 5 4 -7 这个序列最大连续子序列和为14
具体问题见: [url=http://acm.hdu.edu.cn/showproblem.php?pid=1003]HDOJ 1003[/url] [url=http://acm.tzc.edu.cn/acmhome/problemdetail.do?method=showdetail&id=1202]TZU 1202[/url]
这个问题在《数据结构与算法分析--c语言描述》(weiss著)中文版第13页(英文版第18页)中也有描述。在第21页给出了一个算法程序:
int MaxSubsequenceSum(const int A[], int N)
{
int ThisSum,MaxSum,j;
ThisSum = MaxSum = 0;
for(j = 0; j < N; j++)
{
ThisSum += A[j];
if(ThisSum > MaxSum)
MaxSum = ThisSum;
else if(ThisSum < 0)
ThisSum = 0;
}
return MaxSum;
}


我将算法写成了下面的样子:
int MaxSubsequenceSum(const int A[], int N)
{
int ThisSum,MaxSum,j;
ThisSum =0, MaxSum =INT_MIN;
for(j = 0; j < N; j++)
{
ThisSum += A[j];
if(ThisSum > MaxSum)
MaxSum = ThisSum;
if(ThisSum < 0)
ThisSum = 0;
}
return MaxSum;
}


此时必须将else这个关键字删除,这是因为使用了不同的值来初始化变量引起的。书本中的例子能够始终保证MaxSum为非负值。而我改写后的算法在很多情况下MaxSum都会是负数

我的acm程序如下(在上面两个网站都是ac):

#include <stdio.h>
#include <limits.h>

#define MAX 100000+100

int main(void)
{
int n;
int m;
int a[MAX];
int i,j;
int thisSum,maxSum;
int maxStart,maxEnd,thisStart;

scanf("%d",&n);
for(i = 1; i <= n; i++)
{
scanf("%d",&m);
for(maxStart=maxEnd=thisStart=thisSum=0,maxSum=INT_MIN,j = 0; j < m; j++)
{
scanf("%d",&a[j]);
thisSum += a[j];

if(thisSum > maxSum)
{
maxSum = thisSum;
maxStart = thisStart;
maxEnd = j;
}
if(thisSum < 0)
{
thisSum = 0;
thisStart = j+1;
}
}
if(i > 1)
printf("\n");
printf("Case %d:\n",i);
printf("%d %d %d\n",maxSum,maxStart+1,maxEnd+1);
}
return 0;
}

程序主要部分还是上面的算法,只是加上了对子序列首尾索引号的处理。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值