Java (array)

Source: Internet
Author: User

First, the topic requirements:

Enter a one-dimensional shape array with positive and negative numbers in the array.

One-dimensional arrays end-to-end, like a first-to-last 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.

Publish a blog post about design ideas, problems that arise, possible solutions (multiple choices), source code, results, summaries.

Second, design ideas:

Iterate through the array of each number of the first number into the last number, the specific algorithm a[i-1]=a[i], this becomes a new one-dimensional array, output the maximum number of sub-arrays per array, and then compare each output and, find the largest

Three, code

ImportJava.util.Scanner; Public classShuzu { Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner scan=NewScanner (system.in); System.out.print ("Please enter the number of numbers in the array:"); intn=Scan.nextint (); int[] A=New int[n]; System.out.print ("Please enter an array:");  for(inti=0;i<n;i++) {A[i]=Scan.nextint (); }        inta1=Zuida (a,n); intB,temp,i,sum=0;  for(b=1;b<n;b++) {Temp=a[0];  for(i=1;i<=n-1;i++) {A[i-1]=A[i]; } a[n-1]=temp; if(Zuida (A,n) >=sum) {Sum=Zuida (a,n); }        }        intA2=sum; if(a1>=A2) {System.out.println ("Maximum sub-array and:" +A1); }        Else{System.out.println ("Maximum sub-array and:" +A2); }                  }      Public Static intZuida (intA[],intN) {intSum=0; intB=0;  for(inti=0; i<n; i++)            {                if(b<0) b=A[i]; Elseb+=A[i]; if(sum<b) Sum=b; }            returnsum; }}

Four, run

Java (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.