欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++動(dòng)態(tài)規(guī)劃計(jì)算最大子數(shù)組

 更新時(shí)間:2022年06月30日 10:35:57   作者:成就一億技術(shù)人  
所謂最大子數(shù)組就是連續(xù)的若干數(shù)組元素,如果其和是最大的,那么這個(gè)子數(shù)組就稱為該數(shù)組的最大子數(shù)組

例題

題目:輸入一個(gè)整形數(shù)組,數(shù)組里有正數(shù)也有負(fù)數(shù)。數(shù)組中連續(xù)的一個(gè)或多個(gè)整數(shù)組成一個(gè)子數(shù)組,每個(gè)子數(shù)組都有一個(gè)和。求所有子數(shù)組的和的最大值。要求時(shí)間復(fù)雜度為O(n)。

例如輸入的數(shù)組為1, -2, 3, 10, -4, 7, 2, -5,和最大的子數(shù)組為3, 10, -4, 7, 2,因此輸出為該子數(shù)組的和18。

1.求最大的子數(shù)組的和

代碼【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;
}

結(jié)果

2.求和最大的相應(yīng)子數(shù)組

代碼【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;
}

結(jié)果

到此這篇關(guān)于C++動(dòng)態(tài)規(guī)劃計(jì)算最大子數(shù)組的文章就介紹到這了,更多相關(guān)C++最大子數(shù)組內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語言實(shí)現(xiàn)通用數(shù)據(jù)結(jié)構(gòu)之通用映射(HashMap)

    C語言實(shí)現(xiàn)通用數(shù)據(jù)結(jié)構(gòu)之通用映射(HashMap)

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)通用數(shù)據(jù)結(jié)構(gòu)之通用映射,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 深入理解卡特蘭數(shù)及其應(yīng)用

    深入理解卡特蘭數(shù)及其應(yīng)用

    本篇文章是對(duì)卡特蘭數(shù)及其應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言實(shí)現(xiàn)發(fā)牌洗牌

    C語言實(shí)現(xiàn)發(fā)牌洗牌

    這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)發(fā)牌洗牌,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 詳解編譯器編譯原理

    詳解編譯器編譯原理

    這篇文章主要介紹了詳解編譯器編譯原理的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • C語言隨機(jī)數(shù)生成教程(rand和srand用法)

    C語言隨機(jī)數(shù)生成教程(rand和srand用法)

    這篇文章主要介紹了C語言隨機(jī)數(shù)生成教程(rand和srand用法),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • c++中typename和class的區(qū)別介紹

    c++中typename和class的區(qū)別介紹

    在c++Template中,很多地方都用到了typename與class這兩個(gè)關(guān)鍵字,而且好像可以替換,是不是這兩個(gè)關(guān)鍵字完全一樣呢?
    2013-03-03
  • C++實(shí)現(xiàn)管理系統(tǒng)的示例代碼

    C++實(shí)現(xiàn)管理系統(tǒng)的示例代碼

    這篇文章主要介紹了C++實(shí)現(xiàn)管理系統(tǒng)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • c語言左移和右移的示例詳解

    c語言左移和右移的示例詳解

    這篇文章主要介紹了c語言左移和右移的示例詳解,移位操作符的兩個(gè)操作數(shù)必須是整型的。整個(gè)移位表達(dá)式的值的類型也是整型的,而且,左移位操作符與右移位操作符的運(yùn)算并不對(duì)稱。
    2020-07-07
  • 基于Matlab實(shí)現(xiàn)中國象棋的示例代碼

    基于Matlab實(shí)現(xiàn)中國象棋的示例代碼

    中國象棋是起源于中國的一種棋,屬于二人對(duì)抗性游戲的一種,在中國有著悠久的歷史。由于用具簡(jiǎn)單,趣味性強(qiáng),成為流行極為廣泛的棋藝活動(dòng)。本文將利用Matlab實(shí)現(xiàn)這一游戲,需要的可以參考一下
    2022-02-02
  • 解析C++ 浮點(diǎn)數(shù)的格式化顯示

    解析C++ 浮點(diǎn)數(shù)的格式化顯示

    本篇文章是對(duì)C++中浮點(diǎn)數(shù)的格式化顯示進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評(píng)論