C++動(dòng)態(tài)規(guī)劃計(jì)算最大子數(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)文章希望大家以后多多支持腳本之家!
- C++中的動(dòng)態(tài)規(guī)劃子序列問題分析探討
- C++動(dòng)態(tài)規(guī)劃算法實(shí)現(xiàn)矩陣鏈乘法
- C++動(dòng)態(tài)規(guī)劃實(shí)現(xiàn)查找最長(zhǎng)公共子序列
- C++?動(dòng)態(tài)規(guī)劃算法使用分析
- C++編輯距離(動(dòng)態(tài)規(guī)劃)
- c++動(dòng)態(tài)規(guī)劃經(jīng)典算法
- C++動(dòng)態(tài)規(guī)劃之最長(zhǎng)公子序列實(shí)例
- C++動(dòng)態(tài)規(guī)劃之背包問題解決方法
- C++動(dòng)態(tài)規(guī)劃中關(guān)于背包問題講解
相關(guān)文章
C語言實(shí)現(xiàn)通用數(shù)據(jù)結(jié)構(gòu)之通用映射(HashMap)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)通用數(shù)據(jù)結(jié)構(gòu)之通用映射,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11C語言隨機(jī)數(shù)生成教程(rand和srand用法)
這篇文章主要介紹了C語言隨機(jī)數(shù)生成教程(rand和srand用法),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01C++實(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基于Matlab實(shí)現(xiàn)中國象棋的示例代碼
中國象棋是起源于中國的一種棋,屬于二人對(duì)抗性游戲的一種,在中國有著悠久的歷史。由于用具簡(jiǎn)單,趣味性強(qiáng),成為流行極為廣泛的棋藝活動(dòng)。本文將利用Matlab實(shí)現(xiàn)這一游戲,需要的可以參考一下2022-02-02