Find the maximum value of the sum of the neutrons in an array of integers, for example: Array [1,-2,3,5,1], return 8 (because the conforming subarray is [3,5]), array [1,-2,3,-8,5,1], return 6 (because the conforming sub-array is [5,1]); array [1,-2,3,- 2,5,1], returns 7 (because the subarray that meets the requirements is [3,-2,5,1]).
Import Java.util.Scanner;
Import java.io.*;
public class maxarr{
public static void Main (string[] args) {
Int[]a = null;
Scanner input = new Scanner (system.in);
int len=0;
System.out.print ("Please enter the length of the array:");
try{
Len = Integer.parseint (Input.nextline ());
}catch (Exception e) {
}
A = new Int[len];
System.out.println ("a.length =" +a.length);
for (int i=0;i
System.out.print ("Please enter a value:");
A[i] = Input.nextint ();
}
System.out.println ();
System.out.print ("The array is:");
for (int i=0;i
System.out.print (A[i] + "");
}
int sum = 0,max = 0;
for (int i=0;i
sum = 0;
for (int j=i;j
Sum + = A[j];
if (Sum>max)
max = sum;
}
}
System.out.println ();
System.out.println ("The maximum value of the sum of the array is" +max);
}
}
?
The program first specifies the length of an array, then assigns the value, and then finds the maximum value of the sum of the sub-arrays according to the following algorithm
Although this method is somewhat brute force, it is also a good solution to the initial understanding of the algorithm.
Practice numerical calculations