I. Topics and Requirements
Topic:
Returns the maximum number of sub-arrays in an integer array and
Requirements:
1. Enter an array of shapes with positive and negative numbers in the array.
2. One or more integers in the array make up a sub-array, each of which has a and.
3. ask for the maximum value of all sub-arrays. Requires a time complexity of O (n).
Pair programming Requirements:
1. Two pairs complete the programming task.
2. One person is primarily responsible for program analysis, code programming.
3. One person is responsible for code review and Code test plan.
4. Publish a blog post about the process, experience, and how to resolve conflicts between two people (attach a work photo of the development). (As of Thursday March 19 24:00)
Second, the design idea
Beginning to see the program is not very difficult, but because of the time complexity required in the topic is O (n), in order to meet this condition on the basis of a certain difficulty to solve the problem, the use of ordinary traversal to find the largest sub-array method here is not a trial, so you must find a search algorithm can achieve time complexity O (n).
Third, the source code
1 /*======================================================================2 # Author:zhangyapeng3 # e-mail: [Email protected]4 # last MODIFIED:2015-03-17 20:235 # Filename:Demo.cpp6 # Description:count The max sum of the sub Array.7 ======================================================================*/8# include <iostream>9 #defineLENGTH 5//defines the length of an arrayTen using namespacestd; One A //find the maximum value of two digits - intMax (intAintBintc) - { the inttemp = a>b?a:b; - intmax = Temp>c?Temp:c; - returnMax; - } + - //to find the maximum number of sub-arrays + intSelectmaxarr (intArr[],intLeftintRight ) A { at if(left = right)//Nested End Condition - { - if(Arr[left] >0) - returnArr[left]; - Else - return 0; in } - intMaxmidleft,maxmidright;//holds the maximum value of the left and right parts of the midpoint to intTempleft,tempright;//the maximum value for storing the left and right parts of the midpoint + intMaxleft,maxright;//That holds the largest subarray of the left and right parts of the nested layer . - intMid//Store the midpoint of each nested set theMid = (left+right)/2; *Maxmidleft =0; $Maxmidright =0;Panax NotoginsengTempleft =0; -Tempright =0; the for(inti = mid;i >= left;i--)//from the point as the boundary, start with the largest sub-array on both sides and recursively (left part) + { ATempleft + =Arr[i]; the if(Maxmidleft <templeft) + { -Maxmidleft =Templeft; $ } $ } - for(intI=mid+1; i<=right;i++)//from the point as the boundary, start with the largest sub-array on both sides and recursively (right part) - { theTempright + =Arr[i]; - if(Maxmidright <tempright)Wuyi { theMaxmidright =Tempright; - } Wu } - //recursively computes the number of the largest sub-arrays of the left and right parts AboutMaxleft =Selectmaxarr (arr,left,mid); $Maxright = Selectmaxarr (arr,mid+1, right); - returnMax (maxleft,maxright,maxmidleft+maxmidright); - } - A //test function + intMain () the { - intArr[length]; $ intMax//holds the maximum number of sub-arrays and thecout<<"Please enter"<<LENGTH<<"integer (can be positive negative):"; the for(intI=0; i<length;i++) the { theCin>>Arr[i]; - } inmax = Selectmaxarr (ARR,0, length-1); thecout<<"The number of the largest sub-arrays in the array is:"<<max<<Endl; the return 0; About}
Iv. Results of operation
V. Results test
1. Array elements are ordered positive numbers
2. Array elements are random positive numbers
3. Array elements are ordered negative numbers
4. The array element is a disorderly sequence negative number
5. Array elements are positive and negative blends
Vi. Results Test
In this pair development, Tian Yongtao and I have just started to clarify their own views on a topic, everyone's ideas are certainly not exactly the same, so we actively discuss the best solution, if the topic does not consider the time complexity of O (n) can be achieved through grass-roots nesting, the idea is not complex, The reason why it is difficult to solve is because of the insufficient understanding of some fast and efficient search algorithms.
In writing the code, we have a clear division of labor, I write code Tian Yongtao responsible for code errors and related optimizations, so that the code efficiency may not be significantly improved, but some of the code of writing will be implemented very well, the reason why the efficiency is not high may be because the first attempt of this pair development mode, I believe that after this practice more times, the efficiency will be more obvious improvement.
Maximum sub-array summation problem