A title: A one-dimensional integer array with a first-and-last-end, with a positive negative, the largest subarray of its number, and its position returned.
Pair Developer: Zhu Shaohui, Hou Tao Bright
Two design ideas: Set this array to A[n], then expand the array range so that it is a[2*n], the value of a[0]~a[n-1] to a[n]-a[2*n-1], then the equivalent from the first (i=0,1,2..n-1) to I+n (does not include i+ N) the largest sub-array, which can be used to find a one-dimensional array (not the ring) maximal sub-array dynamic planning of the idea of the solution, each to obtain a maximum value for the comparison, and finally the largest sub-array can be obtained. As for the position is the largest sub-array subscript, can be attached to a pointer J to the end of the maximum subarray, the pointer every time, the maximum Max minus a[j], until the max value is 0, you can find the largest sub-array of the first subscript.
Three codes
#include <iostream>#include<cmath>using namespacestd;voidMain () {inta[25000],i,j,n,maxsum,k,m,h,p; intf=0; intb[Ten],d[Ten]; cout<<"Please enter the number of numbers in the array"<<Endl; CIN>>N; cout<<"Please enter an array"<<Endl; for(i=0; i<n;i++) {cin>>A[i]; } for(i=n;i<2*n;i++) {A[i]=a[i-N]; } cout<<Endl; for(i=0; i<n;i++) {k=0; Maxsum=0; H=i; for(j=i;j<i+n;j++) {k+=A[j]; if(k >maxsum) {Maxsum=K; H=J; } if(K <0) {k=0; }} D[i]=h; B[i]=maxsum; } intc=b[0]; M=0; H=0; for(i=1; i<n;i++) { if(c<=B[i]) {C=B[i]; M=i; H=D[i]; }} P=h-n+1; if(p<=0) {p=N;} cout<<"from the first"<<m+1<<"to the first"<<p<<"a"<<Endl; cout<<"the maximum number of sub-arrays is"<<Endl; for(i=m;i1; i++) {cout<<a[i]<<" ";} cout<<Endl; cout<<"The sum of the maximum sub-arrays is"<<c<<Endl; }
Four
Five Summary Reflection
In this pair development, my partner and I think about the design ideas, and then exchange views, reached a consensus of design ideas. This development process not only facilitates communication between us, but also improves our programming ability, which shows the importance of team work.
Array with end-to-end (ring)-Maximum sub-array