Returns the maximum number of sub-arrays in an integer array (enhanced)

Source: Internet
Author: User

Title: Returns the maximum number of sub-arrays in an integer array and

requirement : To connect the end of the array to form a ring, and return the position of the largest sub-array

Design Ideas :

1, the ring is broken, become a line, analysis of the largest sub-array on this line, find the largest sub-array, you can find the largest sub-array start and end.

2, then the end point as a starting point, the starting point as the end point, the line into a ring, in the search for the largest sub-array (not counting the beginning and end of the search).

3, the last found in the two largest sub-arrays joined together to form the largest subarray of this integer ring, and then the record start and end point output, return the maximum sub-array position.

Source:

#include <iostream>#defineN 100using namespacestd;voidMain () {intA[n], b[n][n]; intLength, I, j, W =0, p =0, q =0, temp, m; cout<<"Enter a random integer"<<Endl;  for(length =0;;) {cin>>A[length]; Length++; if(GetChar () = ='\ n')        {             Break; }} cout<<"The length of this array is:"<< length <<Endl; //To find a sub-array     for(i =0; i<length; i++)//two cycles, the elimination method, to determine the maximum number of sub-arrays{m=i; W=0; J=0;  while(J <= Length-1) {W+=A[m]; B[I][J]=W; M++; if(M>length-1) {m=0; } J++; }} Temp= b[0][0];  for(i =0; i<length; i++)//The maximal subarray of each number corresponds to the maximum subarray of the whole integer group.    {         for(j =0; j<length; J + +)        {            if(b[i][j]>temp) {Temp=B[i][j]; P=i; Q=J; } }} cout<<"the maximum number of sub-arrays is:"<< Temp <<Endl; cout<<"The subscript of the element in the maximum sub-array is seated:"<<Endl; I=0;  while(I <=q) {cout<< P <<"  "; P++; if(P >=length) {P=0; } I++; } cout<<Endl;}

Test Results :

Summary: This experiment is mainly to use the idea of ring insertion, need to fully consider the connection problem. So we use a large array to compare the two different starting points respectively.

Teammate: Lu Guanghao website: http://www.cnblogs.com/lvstudy/

Returns the maximum number of sub-arrays in an integer array (enhanced)

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.