#include<iostream>
#include<cstring>
using namespace std;
struct max_sit_subarray
{
int low;
int sum;
int high;
};
//求跨越中点的最大子数组
max_sit_subarray find_max_crossing_subarray(int A[],int low,int mid,int high){
bool flag=true;
int left_sum,max_left;//最大和数组下标
int right_sum,max_right;
int sum=0;
for(int i=mid;i>=low;--i){
sum=sum+A[i];
if(flag){ //是否是第一个数
flag=false;
left_sum=sum;
max_left=mid;
}
else{
if(sum>left_sum){
left_sum=sum;
max_left=i;//最大和数组的左边界
}
}
}
sum=0;//重置sum
flag=true;//重置flag
for(int j=mid+1;j<=high;j++){
sum=sum+A[j];
if(flag){ //是否是第一个数
flag=false;
right_sum=sum;
max_right=mid+1;
}
else{
if(sum>right_sum){
right_sum=sum;
max_right=j;//最大和数组的右边界
}
}
}
max_sit_subarray mss;
mss.low=max_left;
mss.high=max_right;
mss.sum=right_sum+left_sum;
return mss;
}
//求最大子数组
max_sit_subarray find_maximum_subarray(int *A ,int low,int high){
max_sit_subarray mss;
max_sit_subarray left_result,right_result,cross_result;
int mid;
if(low==high){
mss.low=low;
mss.high=high;
mss.sum=A[low];
return mss;
}
else{
mid=(low+high)/2;
left_result=find_maximum_subarray(A,low,mid);
right_result=find_maximum_subarray(A,mid+1,high);
cross_result=find_max_crossing_subarray(A,low,mid,high);
if(left_result.sum>right_result.sum&&left_result.sum>cross_result.sum)
return left_result;
if(right_result.sum>left_result.sum&&right_result.sum>cross_result.sum)
return right_result;
if(cross_result.sum>right_result.sum&&cross_result.sum>left_result.sum)
return cross_result;
}
}
int main(){
int A[]={13,-3,-16,7,15,2,-2,6};
max_sit_subarray result= find_maximum_subarray(A,0,7);
cout<<"low: "<<result.low<<"\t high: "<<result.high<<"\t max_sumarray: "<<result.sum<<endl;
return 0;
}
#include<cstring>
using namespace std;
struct max_sit_subarray
{
int low;
int sum;
int high;
};
//求跨越中点的最大子数组
max_sit_subarray find_max_crossing_subarray(int A[],int low,int mid,int high){
bool flag=true;
int left_sum,max_left;//最大和数组下标
int right_sum,max_right;
int sum=0;
for(int i=mid;i>=low;--i){
sum=sum+A[i];
if(flag){ //是否是第一个数
flag=false;
left_sum=sum;
max_left=mid;
}
else{
if(sum>left_sum){
left_sum=sum;
max_left=i;//最大和数组的左边界
}
}
}
sum=0;//重置sum
flag=true;//重置flag
for(int j=mid+1;j<=high;j++){
sum=sum+A[j];
if(flag){ //是否是第一个数
flag=false;
right_sum=sum;
max_right=mid+1;
}
else{
if(sum>right_sum){
right_sum=sum;
max_right=j;//最大和数组的右边界
}
}
}
max_sit_subarray mss;
mss.low=max_left;
mss.high=max_right;
mss.sum=right_sum+left_sum;
return mss;
}
//求最大子数组
max_sit_subarray find_maximum_subarray(int *A ,int low,int high){
max_sit_subarray mss;
max_sit_subarray left_result,right_result,cross_result;
int mid;
if(low==high){
mss.low=low;
mss.high=high;
mss.sum=A[low];
return mss;
}
else{
mid=(low+high)/2;
left_result=find_maximum_subarray(A,low,mid);
right_result=find_maximum_subarray(A,mid+1,high);
cross_result=find_max_crossing_subarray(A,low,mid,high);
if(left_result.sum>right_result.sum&&left_result.sum>cross_result.sum)
return left_result;
if(right_result.sum>left_result.sum&&right_result.sum>cross_result.sum)
return right_result;
if(cross_result.sum>right_result.sum&&cross_result.sum>left_result.sum)
return cross_result;
}
}
int main(){
int A[]={13,-3,-16,7,15,2,-2,6};
max_sit_subarray result= find_maximum_subarray(A,0,7);
cout<<"low: "<<result.low<<"\t high: "<<result.high<<"\t max_sumarray: "<<result.sum<<endl;
return 0;
}