Emergent inspiration come up with a new simple algorithm--the implementation of the maximum array of arrays and

Source: Internet
Author: User

First, title:

Returns the and of the largest sub-array in an integer array.

Requirements:

1. Enter an array of shapes with positive and negative numbers in the array.

2. One or more integers in the array make up a sub-array, each of which has a and.

3. Ask for the maximum value of all sub-arrays. Requires a time complexity of O (n).

Second, design ideas

First, each point is considered a radiation source, and no point can only be radiated to the right to find out the maximum value of all possible sub-arrays, such as arr[5]={1,2,3,4,5}. First 1 with 1+2 and 1+2+3 ... to 1+2+3+4+5 find the maximum value in the array a[0].

Then 2 to the right to find out all possible sub-arrays to calculate the maximum value of deposit a[1] and so on until the end. The maximum value of the maximum number of sub-arrays for this array in the Find a[5]

Third, the Code


Package Com.minirisoft;import Java.util.*;class supermax{public static void Main (string[] args) {Scanner sc=new Scanner ( system.in); int[] List = new int[10];//The input array is an array that must be defined before an error occurs! int[] arr1 = new int[10];//input array is an array that must be defined before an error occurs! SYSTEM.OUT.PRINTLN ("Please input array:"); for (int k=0;k<10;k++) {list[k]=sc.nextint ();} for (int n=0;n<9;n++) {arr1[n]=list[n];for (int m=n;m<9;m++) {arr1[m+1]=arr1[m]+list[m+1];} for (int m=n;m<10;m++) {if (Arr1[m]>arr1[n]) {arr1[n]=arr1[m];}} System.out.println ("+arr1[n") for the maximum number of sub-arrays ("+n+1+" from the first element);} arr1[9]=list[9];for (int i=0;i<10;i++) {if (arr1[i]>arr1[0]) {arr1[0]=arr1[i];}} System.out.println ("Maximum subarray of" and "+arr1[0]);}}

Four, the experiment

Five, experimental experience

Sometimes sudden inspiration is more important than a hard day of programming. This simple 20-minute program of 41 lines of code is a 8-hour victory over the last experiment.

Emergent inspiration come up with a new simple algorithm--the implementation of the maximum array of arrays and

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.