vc++算法实现最大子段和
#include <iostream> using namespace std; int maxSubItem(int *a,int low,int high) { int s1,s2,s31,s32,i,j; int sum; int mid = ( low + high ) / 2; if(low == high) return a[low]; else { s1 = maxSubItem(a,low,mid); s2 = maxSubItem(a,mid+1,high); i = mid; s31 = a[mid]; while ((s31 + a[i-1] > s31) && (i > low)) { s31 += a[i-1]; i--; } j = mid + 1; s32 = a[mid + 1]; while ((s32 + a[j + 1] > s32) && (j < high)) { s32 += a[j + 1]; j++; } sum = s31 + s32; if(sum < s1) sum = s1; if(sum < s2) sum = s2; } return sum; } void main() { int a[6]= {-2,11,-4,13,-5,-2}; cout<<"The maximum subItem is "<<maxSubItem(a,0,5)<<endl; }