An array of one-dimensional loops to find out the maximum number of sub-arrays

Source: Internet
Author: User

The title is: An array of one-dimensional loops to find out the maximum number of sub-arrays

The teacher just give this topic, find out one-dimensional sub-array of the largest sub-array of the and, at that time I would like to have done a one-dimensional array to find the largest sub-array and can be extended on this basis, so I want to use the original algorithm, Later I thought that since it is still to find the largest sub-array and certainly the original things can be exploited.

I think since it is the loop, nothing is this array is carried out two times, so I feel so that I can apply to the array after the same length, the number of the size and order and its same array, so it played the purpose of the loop, so I did so, and then call the original method, This allows you to find the value of the maximum subarray. So I wrote out the program and the results were found to be wrong. The most sub-array and not the original is not the expected result, so I think the problem is where, and then I found that the original problem is the length of the largest sub-array is a problem, since the length of the array has been enlarged one-fold, so the length of the sub-array may also be changed, At this point I thought of the need to add some restrictions on the length of the sub-array, so that his length is always less than the length of the original array, so that a good solution to the problem of the length of the sub-array, so that the sub-array is in line with the requirements.

 PackageShengcheng;ImportJava.util.Scanner;Importorg.junit.Test; Public classxunhuan{@Test Public  voidFun () {int[]a=New int[10]; intN; Scanner Write=NewScanner (system.in); System.out.println ("Please enter the length of the array to be entered"); N=Write.nextint ();  for(inti=0;i<5;i++) {A[i]=Write.nextint ();            } Xunhuan (A,n); }     Public voidXunhuan (intA[],intN) {inttemp=-200000; intmax=-200000;  for(inti=0;i<n;i++) {A[i+n]=A[i]; }         for(inti=0;i<n;i++) {Temp=-200000;  for(intj=i;j<n+i;j++) {Temp+=A[j]; if(temp>Max) {Max=temp; }             if(temp<0) {Temp=A[j];    }}} System.out.println (Max); }}


Operation Result:

An array of one-dimensional loops to find out the maximum number of sub-arrays

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.