To find the largest subarray and

Source: Internet
Author: User

Title Requirements:

1, enter a one-dimensional shaping array, there are positive numbers in the array and negative numbers.

2, one-dimensional array end-to-end, like a one-end-to-end tape.

3. One or more integers in the array make up a sub-array, each of which has a and. The maximum value for the and of all sub-arrays.

Design ideas:

Iterate through the array of each number of the first number into the last number, the specific algorithm a[i-1]=a[i], this becomes a new one-dimensional array, output the maximum number of sub-arrays per array, and then compare each output and, find the largest

Code:

Import Java.util.scanner;public class Shuzu {public static void main (string[] args) {//TODO auto-generated method Stubsca Nner scan=new Scanner (system.in); System.out.print ("Enter the number of numbers in the array:"); int n=scan.nextint (); int[] A=new int [n]; System.out.print ("Please input array:"); for (int i=0;i<n;i++) {a[i]=scan.nextint ();}        int a1=findmaxsum (a,n); int b,temp,i,sum=0;for (b=1;b<n;b++) {temp=a[0];                 for (i=1;i<=n-1;i++) {a[i-1]=a[i];        } a[n-1]=temp;        if (Findmaxsum (a,n) >=sum) {sum=findmaxsum (a,n);    }} int a2=sum;    if (A1&GT;=A2) {System.out.println ("Maximum sub-array and:" + A1);    } else {System.out.println ("Maximum sub-array and:" + A2);             }} public static int findmaxsum (int a[],int n) {int sum=0;          int b=0;        for (int i=0; i<n; i++) {if (b<0) b=a[i];        else B+=a[i];    if (sum<b) sum=b; } return sum; }}

  

Operation Result:

To find the largest subarray 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.