=================================版权声明=================================
版权声明:本文为博主原创文章 未经许可不得转载
请通过右侧公告中的“联系邮箱([email protected])”联系我
未经作者授权勿用于学术性引用。
未经作者授权勿用于商业出版、商业印刷、商业引用以及其他商业用途。
本文不定期修正完善,为保证内容正确,建议移步原文处阅读。 <--------总有一天我要自己做一个模板干掉这只土豆
本文链接:http://www.cnblogs.com/wlsandwho/p/4710986.html
耻辱墙:http://www.cnblogs.com/wlsandwho/p/4206472.html
=======================================================================
多年来总算自己亲手写了一次。
算法导论真是本好书。
=======================================================================
一直以为跨mid的部分会很难办,其实仔细看书亲手写过代码之后就会发现,也没啥。
=======================================================================
每个函数最后两个引用参数,是为了把最大的边界序号传回调用点。毕竟书上的是伪代码。
=======================================================================
1 #include "stdafx.h" 2 #include <intsafe.h> 3 #include <iostream> 4 5 using namespace std; 6 7 int FindMaxCrossingSubArraybyWLS(int nArray[],int nLeft,int nMid,int nRight,int& nMaxLeftIndex,int& nMaxRightIndex) 8 { 9 int nLeftSum=INT_MIN; 10 int nRightSum=INT_MIN; 11 int nSum=0; 12 13 for (int i=nMid;i>=nLeft;--i) 14 { 15 nSum+=nArray[i]; 16 17 if (nSum>nLeftSum) 18 { 19 nLeftSum=nSum; 20 nMaxLeftIndex=i; 21 } 22 } 23 24 nSum=0; 25 for (int i=nMid+1;i<=nRight;i++) 26 { 27 nSum+=nArray[i]; 28 29 if (nSum>nRightSum) 30 { 31 nRightSum=nSum; 32 nMaxRightIndex=i; 33 } 34 } 35 36 return nLeftSum+nRightSum; 37 } 38 39 int FindMaxSubArraybyWLS(int nArray[],int nLeft,int nRight,int& nMaxLeftIndex,int& nMaxRightIndex) 40 { 41 int nMaxSum=INT_MIN; 42 43 if (nLeft==nRight) 44 { 45 nMaxLeftIndex=nLeft; 46 nMaxRightIndex=nRight; 47 48 nMaxSum=nArray[nLeft]; 49 } 50 else 51 { 52 int nMid=(nLeft+nRight)/2; 53 54 int nLefMaxLeftIndex=0; 55 int nLeftMaxRightIndex=0; 56 int nLeftMaxSubArraySum=FindMaxSubArraybyWLS(nArray,nLeft,nMid,nLefMaxLeftIndex,nLeftMaxRightIndex); 57 58 int nRightMaxLeftIndex=0; 59 int nRightMaxRightIndex=0; 60 int nRightMaxSubArraySum=FindMaxSubArraybyWLS(nArray,nMid+1,nRight,nRightMaxLeftIndex,nRightMaxRightIndex); 61 62 int nMidMaxLeftIndex=0; 63 int nMidMaxRightIndex=0; 64 int nMidMaxSubArraySum=FindMaxCrossingSubArraybyWLS(nArray,nLeft,nMid,nRight,nMidMaxLeftIndex,nMidMaxRightIndex); 65 66 if (nLeftMaxSubArraySum>nMidMaxSubArraySum && nLeftMaxSubArraySum>nRightMaxSubArraySum) 67 { 68 nMaxSum=nLeftMaxSubArraySum; 69 70 nMaxLeftIndex=nLefMaxLeftIndex; 71 nMaxRightIndex=nLeftMaxRightIndex; 72 } 73 else 74 if (nRightMaxSubArraySum>nLeftMaxSubArraySum && nRightMaxSubArraySum>nMidMaxSubArraySum) 75 { 76 nMaxSum=nRightMaxSubArraySum; 77 78 nMaxLeftIndex=nRightMaxLeftIndex; 79 nMaxRightIndex=nRightMaxRightIndex; 80 } 81 else 82 { 83 nMaxSum=nMidMaxSubArraySum; 84 85 nMaxLeftIndex=nMidMaxLeftIndex; 86 nMaxRightIndex=nMidMaxRightIndex; 87 } 88 } 89 90 return nMaxSum; 91 } 92 93 int _tmain(int argc, _TCHAR* argv[]) 94 { 95 // int nArr[7]={1,2,3,-1,4,-2,-3}; 96 // 97 // int nMaxLeftIndex=0; 98 // int nMaxRightIndex=0; 99 // int nMaxSum=FindMaxCrossingSubArraybyWLS(nArr,0,3,6,nMaxLeftIndex,nMaxRightIndex); 100 101 int nArr[16]={13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7}; 102 103 int nMaxLeftIndex=0; 104 int nMaxRightIndex=0; 105 int nMaxSum=FindMaxSubArraybyWLS(nArr,0,15,nMaxLeftIndex,nMaxRightIndex); 106 107 cout<<"nArr["<<nMaxLeftIndex<<"..."<<nMaxRightIndex<<"] = "<<nMaxSum<<endl; 108 109 return 0; 110 }
=======================================================================
一直反对abcijklmnpq的命名,所以我一般只在循环里用i。
=======================================================================
千里黄云白日曛,北风吹雁雪纷纷。