Software Engineering Sub-array summation 2

Source: Internet
Author: User

1. Design ideas: First of all think of the difference between the end and the unconnected? The difference between the last number and the previous number can be a new array;

Do you want to add other restrictions to the array to store the concatenated array?

2. Problem: The end-to-end array cannot be stored in an array like any other array.

4. Source code:

Package sub-array summation 2;

Import Java.util.Scanner;

public class Test {

public static void Main (string[] args) {
TODO auto-generated Method Stub
Scanner scan=new Scanner (system.in);
System.out.print ("Please 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;
}
}

Software Engineering Sub-array summation 2

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.