Array class exercises, array classes
Design Philosophy:
There are positive and negative numbers in the array. traverse the array to find the first positive number and set it to the maximum value. Start to add backward. Once the sum is negative, the number of the front edge is discarded. Continue to add backward from the current position. After each addition, It is compared with the maximum value. If it is smaller than the maximum value, it is not recorded, otherwise, the record is the maximum value.
Code:
Import java. util. random; public class ArrayAdd {public static void main (String args []) {int a [] = new int [10]; for (int I = 0; I <10; I ++) {a [I] = new Random (). nextInt () % 30; System. out. print (a [I] + "");} System. out. println (); int max = 0; int Sum = 0; int Start = 0; int start = 0; int end = 0; for (int I = 0; I <10; I ++) {if (I = 0) {Sum = max = a [I]; continue;} if (Sum <0) {Sum = 0; start = I;} Sum + = a [I]; if (Sum> max) {max = Sum; start = Start; end = I ;}} System. out. println ("and the largest sub-array is:"); for (int I = start; I <= end; I ++) {System. out. print (ary [I] + "+");} System. out. print ("=" + max );}}
:
Summary:
To solve the problem, learn mathematics first.