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)