Integer array to find the maximum number of sub-arrays and

Source: Internet
Author: User

First, the experimental topic

Returns the and of the largest sub-array in an integer array.

Second, the experimental requirements

Enter a one-dimensional shape array with positive and negative numbers in the array.
One-dimensional arrays end-to-end, like one end-to-end tape.
One or more consecutive integers in an array make up a sub-array, each of which has a and. The maximum value for the and of all sub-arrays.

Third, the design idea

Most of the experiment can be divided into two parts:

1. Use the while loop input array value, input value is string type, use Integer.parseint (String a), deposit int Array, if "#" then end input

2, call the maximum function and the maximum value, divided into two parts:

1, first to find a linear array of the continuous and maximum value, the algorithm complexity of O (n) (1) If a,b>0, then a+b affirmation >a. (2) If a,b>0,c<0, so a+b+c certainly <a+b. (3) If a<0,b>0, so B must be greater than a+b. So the current one and small and zero, the assignment is zero, plus the next number, if not compared to the maximum value, if greater than the maximum value, the maximum value is updated. When the last sum=0, there are only two cases in which the continuous and maximum values in the array are 0, and the other is that all the numbers are less than 0.

2, the continuous and maximum value of the ring, the next of the last item is the first number, that is, the subscript out of range minus the length of the array. Executes n times with a for loop, traversing the final maximum value, and then outputting.

Iv. problems that arise

1, when the loop traversal n times, the cohesion is not correct.

V. Possible solutions (multiple selection)

List all cases, find out and, select the maximum value output.

Vi. Source Code

ImportJava.util.Scanner;PublicClassMax {PublicStaticvoidMain (string[] args) {int shu[]=Newint[100];int k=0; Scanner scanner=NewScanner (system.in); System.out.println ("Please enter a number of integers:"); String a=Scanner.next ();if (A.equals ("#")) System.out.println ("You have nothing to enter, so there is no maximum value!") ");Else{shu[k]=Integer.parseint (a); k++; A=Scanner.next ();while (!a.equals ("#")) {shu[k]=Integer.parseint (a); k++; A=Scanner.next (); } System.out.println ("The maximum number of consecutive numbers in an array is:" +Maxsum (shu,k)); } }Staticint Maxsum (Int[] arr,IntK) {int sum=0;int maxsum=0;int len=KIntDIntIIntJfor (j=0;j<len;j++) {for (i=j;i<len+j;i++) {if (i>len-1) d=i-Lenelse d=i; Sum+=arr[d]; if (Sum<0if (Sum>maxsum) {maxsum= Sum; }} Sum=0;} if (Maxsum==0for (i=0;i <len;i++) {if (I==0arr[i];} if (Arr[i]>maxsum) {maxsum= arr[i];} }} return Maxsum;}}       

Vii. Results

Viii. Summary

This program does not perform the final optimization.

Integer array to find the maximum number of sub-arrays and

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.