#include <iostream>
#include <string>
using namespace std;
//枚举
int MaxSum(int n,int *a,int &besti,int &bestj){
int sum=0;
for(int i=0;i<n;i++){
int thissum=0;
for (int j=i;j<n;j++){
thissum+=a[j];
if (thissum>sum){
sum=thissum;
besti=i;
bestj=j;
}
}
}
return sum;
}
//分治
int MaxSubSum(int *a, int left, int right)
{
int sum=0;
if(left==right)
sum=a[left]>0?a[left]:0;
else{
int mid=(left+right)/2;
int leftsum=MaxSubSum(a,left,mid);
int rightsum=MaxSubSum(a,mid+1,right);
in
分别运用枚举、分治、动态规划进行最大子段和求解
最新推荐文章于 2024-09-10 15:28:21 发布