C++动态规划计算最大子数组

 更新时间:2022年06月30日 10:35:57   作者:成就一亿技术人  
所谓最大子数组就是连续的若干数组元素,如果其和是最大的,那么这个子数组就称为该数组的最大子数组

例题

题目:输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。求所有子数组的和的最大值。要求时间复杂度为O(n)。

例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,因此输出为该子数组的和18。

1.求最大的子数组的和

代码【C++】

#include <iostream>
using namespace std;
/
// Find the greatest sum of all sub-arrays
// Return value: if the input is valid, return true, otherwise return false
/
bool FindGreatestSumOfSubArray
    (
    int *pData,           // an array
    unsigned int nLength, // the length of array
    int &nGreatestSum     // the greatest sum of all sub-arrays
    )
{
    // if the input is invalid, return false
    if((pData == NULL) || (nLength == 0))
        return false;
    int nCurSum = nGreatestSum = 0;
    for(unsigned int i = 0; i < nLength; ++i)
    {
        nCurSum += pData[i];
        // if the current sum is negative, discard it
        if(nCurSum < 0)
            nCurSum = 0;
        // if a greater sum is found, update the greatest sum
        if(nCurSum > nGreatestSum)
            nGreatestSum = nCurSum;
    }
    // if all data are negative, find the greatest element in the array
    if(nGreatestSum == 0)
    {
        nGreatestSum = pData[0];
        for(unsigned int i = 1; i < nLength; ++i)
        {
            if(pData[i] > nGreatestSum)
                nGreatestSum = pData[i];
        }
    }
    return true;
}
int main()
{
    int arr[] = {1, -2, 3, 10, -4, 7, 2, -5};
    int iGreatestSum;
    FindGreatestSumOfSubArray(arr, sizeof(arr)/sizeof(int), iGreatestSum);
    cout << iGreatestSum << endl;
    return 0;
}

结果

2.求和最大的相应子数组

代码【C++】

#include <iostream>
using namespace std;
/
// Find the greatest sum of all sub-arrays
// Return value: if the input is valid, return true, otherwise return false
/
bool FindGreatestSumOfSubArray
    (
    int *pData,           // an array
    unsigned int nLength, // the length of array
    int &nGreatestSum,    // the greatest sum of all sub-arrays
    int &start,                            // Added
    int &end                            // Added
    )
{
    // if the input is invalid, return false
    if((pData == NULL) || (nLength == 0))
        return false;
    int nCurSum = nGreatestSum = 0;
    int curStart = 0, curEnd = 0;        // Added
    start = end = 0;                    // Added
    for(unsigned int i = 0; i < nLength; ++i)
    {
        nCurSum += pData[i];
        curEnd = i;                        // Added
        // if the current sum is negative, discard it
        if(nCurSum < 0)
        {
            nCurSum = 0;
            curStart = curEnd = i + 1;    // Added
        }
        // if a greater sum is found, update the greatest sum
        if(nCurSum > nGreatestSum)
        {
            nGreatestSum = nCurSum;
            start = curStart;            // Added
            end = curEnd;                // Added
        }
    }
    // if all data are negative, find the greatest element in the array
    if(nGreatestSum == 0)
    {
        nGreatestSum = pData[0];
        start = end = 0;                // Added
        for(unsigned int i = 1; i < nLength; ++i)
        {
            if(pData[i] > nGreatestSum)
            {
                nGreatestSum = pData[i];
                start = end = i;        // Added
            }
        }
    }
    return true;
}
int main()
{
    int arr[] = {1, -2, 3, 10, -4, 7, 2, -5};
    int iGreatestSum, start, end;
    FindGreatestSumOfSubArray(arr, sizeof(arr)/sizeof(int), iGreatestSum, 
        start, end);
    cout << iGreatestSum << ": ";
    for(int i = start; i <= end; i++)
    {
        cout << arr[i] << " ";
    }
    return 0;
}

结果

到此这篇关于C++动态规划计算最大子数组的文章就介绍到这了,更多相关C++最大子数组内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • C++实现的分布式游戏服务端引擎KBEngine详解

    C++实现的分布式游戏服务端引擎KBEngine详解

    这篇文章主要详细介绍了C++实现的分布式游戏服务端引擎KBEngine的概念以及使用方法,非常的实用,有需要的小伙伴可以参考下
    2015-03-03
  • C语言中指针常量和常量指针的区别

    C语言中指针常量和常量指针的区别

    本文主要介绍了C语言中指针常量和常量指针的区别,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • Qt模仿Windows文件夹缩略图的三种实现方式

    Qt模仿Windows文件夹缩略图的三种实现方式

    本文讲的不是简单的model/view或者widget的或者QML的基础框架实现,而是在这些框架之上的肉(文件夹缩略图)的效果实现,本文将以QWidget、Qt Quick(QML)、以及QGraph三种实现方式来讲解,如何做出和Windows类似的缩略图,需要的朋友可以参考下
    2024-04-04
  • C++实现LeetCode(127.词语阶梯)

    C++实现LeetCode(127.词语阶梯)

    这篇文章主要介绍了C++实现LeetCode(127.词语阶梯),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • wxWidgets实现图片和文件按钮

    wxWidgets实现图片和文件按钮

    这篇文章主要为大家详细介绍了wxWidgets实现图片和文件按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • C++浅析类与对象基础点

    C++浅析类与对象基础点

    类和对象是两种以计算机为载体的计算机语言的合称。对象是对客观事物的抽象,类是对对象的抽象。类是一种抽象的数据类型;变量就是可以变化的量,存储在内存中—个可以拥有在某个范围内的可变存储区域
    2022-07-07
  • Qt调用MATLAB引擎混合编程的过程详解

    Qt调用MATLAB引擎混合编程的过程详解

    这篇文章给大家介绍了Qt调用MATLAB引擎混合编程的全过程,文中通过图文结合的方式给大家介绍的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下
    2024-01-01
  • C++在同一对象中存储左值或右值的方法

    C++在同一对象中存储左值或右值的方法

    C++ 代码似乎经常出现一个问题:如果该值可以来自左值或右值,则对象如何跟踪该值?即如果保留该值作为引用,那么就无法绑定到临时对象,本文给大家介绍了C++在同一对象中存储左值或右值的几种方法,需要的朋友可以参考下
    2025-03-03
  • C语言结构体详细图解分析

    C语言结构体详细图解分析

    C 数组允许定义可存储相同类型数据项的变量,结构是 C 编程中另一种用户自定义的可用的数据类型,它允许你存储不同类型的数据项,本篇让我们来了解C 的结构体
    2022-03-03
  • C语言常用占位符的使用小结

    C语言常用占位符的使用小结

    占位符是一种用于格式化输出的特殊字符,通常用于 printf() 等输出函数中,本文主要介绍了C语言常用占位符的使用小结,非常具有实用价值,需要的朋友可以参考下
    2023-05-05

最新评论