#include <iostream>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int makeSumMax(const int *,int,int);
int main(int argc, char** argv)
{
int a[7]={-1,-4,19,-6,-8,90,-7};
int *ptr=a;
cout<<makeSumMax(ptr,0,6);
return 0;
}
int makeSumMax(const int *ptr,int left,int right)
{
if(left==right)return ptr[left];
int center=(left+right)/2;
int maxLeft=makeSumMax(ptr,left,center);//左边最大值 ,和下面的一样她会伸到只剩两个数,比较她们的值和她们的和那个大,return回去。然后就慢慢的往上爬 。两边的maxleft和maxright是对应的在三个对比在return。paa爬。到了最上层就结束了。
int maxRight=makeSumMax(ptr,center+1,right);//右边最大值
int t_maxLeftHalf=0, maxLeftHalf=0;
for(int i=center;i>=left;i--)
{
t_maxLeftHalf+=ptr[i];
if(t_maxLeftHalf>maxLeftHalf)
{
maxLeftHalf=t_maxLeftHalf;
}
}//左半边最大的值就是maxleftHalf;
int t_maxRightHalf=0, maxRightHalf=0;
for(int i=center+1;i<=right;i++)
{
t_maxRightHalf+=ptr[i];
if(t_maxRightHalf>maxRightHalf)
{
maxRightHalf=t_maxRightHalf;
}
}//右半边最大的值就是maxleftHalf;
int maxHalf= maxLeftHalf+maxRightHalf;
int max=(maxHalf>maxLeft)?maxHalf:maxLeft;
if(max>maxRight)return max;
else return maxRight;
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int makeSumMax(const int *,int,int);
int main(int argc, char** argv)
{
int a[7]={-1,-4,19,-6,-8,90,-7};
int *ptr=a;
cout<<makeSumMax(ptr,0,6);
return 0;
}
int makeSumMax(const int *ptr,int left,int right)
{
if(left==right)return ptr[left];
int center=(left+right)/2;
int maxLeft=makeSumMax(ptr,left,center);//左边最大值 ,和下面的一样她会伸到只剩两个数,比较她们的值和她们的和那个大,return回去。然后就慢慢的往上爬 。两边的maxleft和maxright是对应的在三个对比在return。paa爬。到了最上层就结束了。
int maxRight=makeSumMax(ptr,center+1,right);//右边最大值
int t_maxLeftHalf=0, maxLeftHalf=0;
for(int i=center;i>=left;i--)
{
t_maxLeftHalf+=ptr[i];
if(t_maxLeftHalf>maxLeftHalf)
{
maxLeftHalf=t_maxLeftHalf;
}
}//左半边最大的值就是maxleftHalf;
int t_maxRightHalf=0, maxRightHalf=0;
for(int i=center+1;i<=right;i++)
{
t_maxRightHalf+=ptr[i];
if(t_maxRightHalf>maxRightHalf)
{
maxRightHalf=t_maxRightHalf;
}
}//右半边最大的值就是maxleftHalf;
int maxHalf= maxLeftHalf+maxRightHalf;
int max=(maxHalf>maxLeft)?maxHalf:maxLeft;
if(max>maxRight)return max;
else return maxRight;
}