Design ideas:
An array to find and the largest sub-array, I think it is to look for a can produce and the largest sub-array of an array of the largest arrangement, that is, to remove the smallest negative number. If the array is positive, all the numbers add up and the maximum, if there are negative numbers in the array, the smallest negative number is removed, and the remaining sub-array is found in one and the largest sub-array. The method of removing the minimum negative number is to iterate over the array, find the minimum value, place it at the top of the array or the bottom.
Source:
ImportJava.util.*; Public classArrayMax { Public Static voidMain (String args[]) {Scanner input=NewScanner (system.in); System.out.print ("Please enter the length of the array:"); intn =Input.nextint (); intData[] =New int[n]; intArray[] =New int[n]; System.out.print ("Please enter an array:"); for(inti=0;i<n;i++) {Data[i]=Input.nextint (); } intsum = 0; intMin = data[0]; intNumber = 0; for(inti=0;i<n;i++) { if(min>Data[i]) {min=Data[i]; number=i; } } for(inti=number;i<n;i++) {Array[i-number] =Data[i]; } for(inti=0;i<number;i++) {Array[i+n-number] =Data[i]; } intMax =min; for(inti=0;i<n;i++) {sum= Sum +Array[i]; if(sum>max) {Max=sum; } if(sum<0) {sum= 0; }} System.out.println ("The maximum value of all sub-arrays is:" +max); }}
Results:
Experience:
This time also found a problem, that is, the last code I did not consider the array all the numbers are negative, the code to calculate the part of the changes to solve the problem.
Class Assignment 04