最大连续子序列和问题

问题描述:给定一个序列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;
}

程序主要部分还是上面的算法,只是加上了对子序列首尾索引号的处理。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546160.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

js 数组添加n次相同元素_数组中两次出现相同元素之间的最大距离

js 数组添加n次相同元素Prerequisite: Hashing data structure 先决条件&#xff1a; 哈希数据结构 Problem statement: 问题陈述&#xff1a; Find maximum distance between two occurrences of same element in the array. 查找两次出现的相同元素在数组中的最大距离。 E…

一道题决定去留:为什么synchronized无法禁止指令重排,却能保证有序性?

前几天有一位读者找我问一个问题&#xff0c;说是这道题可能影响了他接下来3年的技术成长。据说这位读者前面的很多问题会的都还可以&#xff0c;属于那种可过可不过的类型的&#xff0c;面试官出了最后一道题&#xff0c;就是回答的满意就可以给Offer&#xff0c;回答的不好就…

haskell程序设计语言

根据[urlhttp://www.haskell.org/haskellwiki/Haskell]haskell[/url]的[urlhttp://www.haskell.org/haskellwiki/Introduction]官方定义[/url]&#xff0c;haskell是polymorphically(多态&#xff09; statically typed静态类型&#xff09;, lazy&#xff08;懒计算&#xff0…

【Android开发】之Fragment与Acitvity通信

上一篇我们讲到与Fragment有关的常用函数&#xff0c;既然Fragment被称为是“小Activity”&#xff0c;现在我们来讲一下Fragment如何与Acitivity通信。如果上一篇还有不懂得&#xff0c;可以再看一下。传送门。 Fragment与Activity通信的方式如下&#xff1a; 一、通过初始化函…

next和hasnext_使用Java中的next()和hasNext()方法遍历List元素

next和hasnextGiven a List of integers and we have to traverse, print all its element using next() and hasNext() methods. 给定一个整数列表&#xff0c;我们必须遍历&#xff0c;使用next()和hasNext()方法打印其所有元素。 什么是hasNex()和next()方法&#xff1f; (…

「递归」的正确打开方式,看不懂你打我~

这是磊哥的第 189 期分享作者 | 田小齐来源 | 码农田小齐&#xff08;ID&#xff1a;NYCSDE&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;前言 递归&#xff0c;是一个非常重要的概念&#xff0c;也是面试中非常喜欢考的。因为它不但能考察…

Log4cpp 使用手册

参考资料&#xff1a; log4cpp 配置 与 使用http://www.cnblogs.com/welkinwalker/archive/2011/06/23/2088197.html 便利的开发工具-log4cpp快速使用指南 http://www.ibm.com/developerworks/cn/linux/l-log4cpp/ Log4cpp配置文件格式说明 http://sogo6.iteye.com/blog/115431…

适合初学编程的一些项目

学习了一门语言以及数据结构之后&#xff0c;通常需要做一些项目来巩固所学的知识&#xff0c;我感觉最好是用写一些简单的小工具或者小游戏&#xff0c;能够提高自己的编程能力&#xff0c;也能进一步提高自己学习的兴趣。最好的是将自己想做的事情用程序的实现&#xff0c;比…

python 5的倍数_查找所有低于1000的数字的和,这是Python中3或5的倍数

python 5的倍数Sometimes, we need to find the sum of all integers or numbers that are completely divisible by 3 and 5 up to thousands, since thousands are a too large number that’s why it becomes difficult for us. So, here we will do it in Python programmi…

switch 的性能提升了 3 倍,我只用了这一招!

这是我的第 190 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;上一篇《if快还是switch快&#xff1f;解密switch背后的秘密》我们测试了 if 和 switch 的性能&am…

HashMap get不出对象时出错 解决

为什么80%的码农都做不了架构师&#xff1f;>>> 如题&#xff1a; Map map new HashMap(); map.put("1", "A"); map.put("2", "A"); map.put("3", "A"); map.put("4", "A")…

安装codeblocks和wxwidgets及opencv

codeblocks 是一款开放源代码的跨平台的c/c++集成开发环境,它是用wxwidgets 开发的,并且支持插件,功能很强大。可以用来在windows开发各种程序。下面记录一下我安装该软件的过程: 首先到http://www.codeblocks.org/downloads 下载该软件,选择含mingw的文件(codeblocks-10…

Java LinkedList boolean addAll(int index,Collection c)方法,带有示例

LinkedList boolean addAll(int index&#xff0c;Collection c)方法 (LinkedList boolean addAll(int index, Collection c) method) This method is available in package java.util.Collection and here, Collection is an interface. 该方法在java.util.Collection包中可用…

高质量SQL的30条建议!(后端必备)

这是我的第 191 期分享作者 | 捡田螺的小男孩来源 | 捡田螺的小男孩&#xff08;ID&#xff1a;gh_873ad5979a0b&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;本文将结合实例demo&#xff0c;阐述30条有关于优化SQL的建议&#xff0c;多数…

滤波电容的选择(调试中)

整个系统高电平时12V&#xff0c;信号是ms级的负脉冲&#xff0c;脉冲的下降沿很陡&#xff0c;当用示波器展开看的时候&#xff0c;发现了低于0V的过冲&#xff0c;能低到-6V&#xff08;相当严重&#xff09;&#xff0c;这是在整个箱子的出口处测量到的。在主控板&#xff0…

sicp

sicp (structure and interpretion of computer programs &#xff0c;编号为6.001 )是麻省理工学院的一门经典计算机入门课程&#xff0c;虽然近年被另外一门课程所取代&#xff08;6.00 或6.01 &#xff09;&#xff0c;但最近好像有复活 的迹象。Perter Norvig 写了关于sicp…

Redis的自白:我为什么在单线程的这条路上越走越远?

这是我的第 192 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;我是 Redis&#xff0c;今年 11 岁了~曾几何时我是辣么的单纯&#xff0c;辣么的可爱&#xff0c;而如…

两个矩阵相乘的乘法次数_C ++程序将两个数字相乘而不使用乘法运算符

两个矩阵相乘的乘法次数The problem is we have two integer numbers and find the multiplication of them without using the multiplication operator. This problem can be solved using the Russian peasant algorithm. Assume the two given numbers are m and n. Initia…

关于引用

2019独角兽企业重金招聘Python工程师标准>>> 1、 <!-- lang: html --> <!DOCTYPE HTML> <!-- lang: html --> <html> <!-- lang: html --> <head> <!-- lang: html --> <meta charset"utf-8" /> <!--…

ruby array_在Ruby中使用Array.delete()和Array.delete_at()从Array中移除元素

ruby arrayRuby Array.delete()和Array.delete_at()方法 (Ruby Array.delete() and Array.delete_at() methods) In the last article, we have seen how we can remove the elements from an instance of Array class with the help of Array.pop() and Array.shift()? 在上一…