- Design ideas
(1) First set the number of numbers to be entered and the number of inputs;
(2) The maximum value of the array number[], applied to the dynamic programming of the array, will be used to compare two variables, one for the sub-array and the other for the maximum value of the Subarray; Maxnum=max{temp,maxnum} ; temp and Maxnum are initialized to the first number of values;
(3) The value of each number is retrieved sequentially, each time the value is added to the value of the next number and the value is assigned to the variable temp, and if Temp<0;temp is added to the next number, the temp will be smaller, and temp=0 will not affect the sum of the next sub-array; if temp> 0;temp+=number[i],maxnum=max{temp,maxnum};
(4) The array is concatenated to the maximum value of the array of neutrons: the main is to see the last number in the array, if temp plus the last number, temp==maxnum; =maxnum will stop the traversal, as the continuation of the traversal is equivalent to a two-time traversal from the beginning;
(5) in the loop traversal, the length of the Addend group of temp cannot exceed the length of the array, each time the temp is iterated and added, the value of I is saved to a variable, which is used when the second pass checks whether the array length is exceeded.
2.
The maximum value in the package integer array; import java.util.*;p ublic class ArrayMax {public static void main (String args[]) {int number[]=new in T[5]; @SuppressWarnings ("resource") Scanner scanner=new Scanner (system.in); int n; System.out.println ("Please enter the number you want to enter:"); N=scanner.nextint (); System.out.println ("Please enter the number to be entered:"); for (int i=0;i<n;i++) {number[i]=scanner.nextint (); } int max=number[0]; int temp=number[0]; int a=0; for (int i=1;i<2*n;i++) {if (i<n) {if (temp<0) {temp=0; A=1+i; } Temp+=number[i]; if (Temp<max) {a=i+1; } if (Temp>max) {max=temp; }} if (I>n-1&&max==temp) {if (i%n==a) {break; } if (temp<0) {temp=0; } temp+=number[i%n]; if (Temp>max) {max=temp;}}} System.out.println (the maximum subarray value is: "+max+". ");}}
3. Results
The maximum value of a neutron array in an array of integers 2