Title Requirements:
1, enter a one-dimensional shaping array, there are positive numbers in the array and negative numbers.
2, one-dimensional array end-to-end, like a one-end-to-end tape.
3. One or more integers in the array make up a sub-array, each of which has a and. The maximum value for the and of all sub-arrays.
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
Code:
Import Java.util.scanner;public class Shuzu {public static void main (string[] args) {//TODO auto-generated method Stubsca Nner scan=new Scanner (system.in); System.out.print ("Enter the number of numbers in the array:"); int n=scan.nextint (); int[] A=new int [n]; System.out.print ("Please input array:"); for (int i=0;i<n;i++) {a[i]=scan.nextint ();} int a1=findmaxsum (a,n); int b,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 (Findmaxsum (a,n) >=sum) {sum=findmaxsum (a,n); }} int a2=sum; if (A1>=A2) {System.out.println ("Maximum sub-array and:" + A1); } else {System.out.println ("Maximum sub-array and:" + A2); }} public static int findmaxsum (int a[],int n) {int sum=0; int b=0; for (int i=0; i<n; i++) {if (b<0) b=a[i]; else B+=a[i]; if (sum<b) sum=b; } return sum; }}
Operation Result:
To find the largest subarray and