Array with end-to-end (ring)-Maximum sub-array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.