Array class exercises, array classes

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.