#include<stdio.h>int MaxSubSum(int A[],int left,int right);void main(){int A[6]={-2,11,-4,13,-5,99};int max=MaxSubSum(A,0,5);printf("最大字段和为:");printf("%dn",max);}int MaxSubSum(int A[],int left,int right){int leftsum=0;int rightsum=0;int s1=0;int s2=0;int i;intcenter=(left+right)/2;if(left==right){if(A[left]<0)//题目要求最低值就是0return 0;elsereturn A[left];}leftsum=MaxSubSum(A,left,center);//递归左子问题rightsum=MaxSubSum(A,center+1,right);//递归右子问题s1=A[center];//存放从中间开始到左边的最大连续子段s2=A[center+1];//存放从中间开始到右边的最大连续子段int temp1=A[center];//临时变量int temp2=A[center+1];for( i=center-1;i>=0;i--)//求出S1的最大连续值{temp1=A[i]+temp1;//从中间到左边一直求值if(s1<temp1)s1=temp1;//从中间到左边一直求值,s1为最大值}for( i=center+2;i<=right;i++)//求出S2的最大连续值{temp2=A[i]+temp2;if(s2<temp2)s2=temp2;}int sum=s1+s2;if(leftsum>sum)sum=leftsum;if(rightsum>sum)sum=rightsum;return sum;//返回该递归中最大的哪一个值}
如果觉得《lfu算法实现c语言_分治算法解决最大子段和问题。c语言实现》对你有帮助,请点赞、收藏,并留下你的观点哦!