The sum of maximal sub-arrays in the ring integer group in class practice

Source: Internet
Author: User

 design ideas:
There is a class practice before, is the user input an integer group, the sum of the largest sub-arrays, I draw on the other classmate's code, only on this basis to modify.
Using Try,catch makes it unnecessary for the user to enter the length of the array before entering the array, but it requires a number other than an integer to end. A total of 3 loops were used, using a loop to derive the maximum of the sum of the largest sub-arrays in each array , one for
Calculates the sum of the largest subarray and the last one to implement the array's forward movement. The first loop is wrapped in the other two loops.
In general, the first number of each number in an array is added to the top of the previous class practice, and the maximum value is obtained from it.

Program code:
1 Importjava.util.InputMismatchException;2 ImportJava.util.Scanner;3 Public classMaxshuzu {4 Public Static voidMain (String args[])5 {6 intJishuqi=0;7 //define a maximum value8 intMax=0;9 //define an array shuzu[100], 100 can be modifiedTen intshuzu[]=New int[100]; OneScanner in=NewScanner (system.in); ASystem.out.print ("Please enter a group of numbers, ending with a non-integer:"); - //input to end anything except an integer - while(true) the { - //Verify that the input is valid as the end of the loop condition - Try - { +shuzu[jishuqi]=in.nextint (); - } + Catch(inputmismatchexception e) A { at Break; - } - -jishuqi++; - } - //This loop is to derive the maximum value of the sum of the largest sub-arrays in each array in for(intm=0;m<jishuqi;m++) - { to intMaxsum=0,sum=0; + //calculates the sum of the largest subarray - for(inti=0;i<jishuqi;i++) the { * if(sum<0) $ {Panax NotoginsengSum=0; - } thesum+=Shuzu[i]; + if(sum>maxsum) A { themaxsum=sum; + } - } $ //implementing the forward movement of an array $ intA=shuzu[0]; - for(intn=0;n<jishuqi-1;n++) - { theShuzu[n]=shuzu[n+1]; - }Wuyishuzu[jishuqi-1]=A; the - if(maxsum>MAX) Wu { -max=maxsum; About } $ } - //Output Results -System.out.println ("The maximum number of sub-arrays is:" +MAX); - } A}
results:
Program SummaryAfter the program changes, the result is not correct, can not quickly find the problem, the reason, the original is not a module to do unit testing. The reason for the code is to define the maximum value when it is defined outside the loop.

The sum of maximal sub-arrays in the ring integer group in class practice

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.