?? 最大子序列和,動態規劃.txt
字號:
#include <iostream>
using namespace std ;
void MaxSubSum(int nums[], int count, int &resStart, int &resEnd, int &resMax)
{
int start, max;
int i;
start = resStart = resEnd = 0; //初始化當前子序列和最大子序列為nums[0]
max = resMax = nums[0];
for (i = 1; i < count; ++i) {
if (max > 0)
{
max += nums[i];
}
else
{
max = nums[i]; //拋棄當前子序列
start = i; //開始新的子序列搜索
}
if (resMax < max)
{ //更新最大子序列
resMax = max;
resStart = start;
resEnd = i;
}
}
return;
}
int main()
{
int begin,end,max;
int a[]={4,-4,1,2};
MaxSubSum(a,4,begin,end,max);
cout << "原來序列為: " ;
for( int j = 0 ; j < 4 ; j ++)
cout << a[j] << " " ;
cout << endl ;
cout << "最大和為:" << max << endl;
cout << "和最大序列為: " ;
for(int i = begin ; i <= end ;i ++)
cout << a[i] << " " ;
cout << endl ;
return 1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -