Integer Subarray max and add validation

Source: Internet
Author: User

Experimental requirements:

Requires that the program be able to handle up to three elements

Each element is of type int32, and the sum of the sub-arrays is greater than the maximum range of the shaping representation.

Design ideas:

First, the array overflow processing, from small to large start validation, until the program processing time is longer.

The maximum number of integer tests supported by the program is then performed, because the maximum numbers that appear in the result or procedure are unpredictable, so you only need to determine the maximum range of random numbers.

Test results:

Three data:

Five data:

Four data:

Five data:

99999999-999999999

999999999-9999999999

Package sub-array changes ;

Import Java.util.Scanner;

public class Zsz {

/**

* @param args

*/

public static void Main (string[] args) {

TODO auto-generated Method Stub

Scanner a=new Scanner (system.in);

System.out.println (" Please enter array length:");

int N=a.nextint ();

System.out.println (" Enter array value range:");

int N=a.nextint ();

int M=a.nextint ();

int a[]=new Int[n];

int b[][]=new Int[n][n];

int i;

System.out.println (" array is:");

for (i=0;i<n;i++)

{

a[i]=n+ (int) (Math.random () * (m-n));

System.out.print (a[i]+ "\ t");

}

System.out.println ();

int j,k=n,t;

for (i=0;i<n;i++)

{

for (j=0;j<k;j++)

{

for (t=j;t<j+i+1;t++)

{

B[I][J]+=A[T];

}

}

k--;

}

System.out.println (" each sub-array size:");

for (i=0;i<n;i++) {

for (j=0;j<n;j++) {

System.out.print (b[i][j]+ "\ t");

}

System.out.println ();

}

int p=b[0][0];

int q=0,r=0;

for (i=0;i<n;i++)

{

for (j=0;j<n-i;j++)

{

if (P<b[i][j])

{

P=B[I][J];

Q=i;

R=j;

}

}

}

System.out.println ("\ n") The array of neutrons and the largest is:"+p);

System.out.println (" This sub-array is:");

for (i=0;i<q+1;i++)

{

System.out.print (a[r+i]+ "\ t");

}

}

}

Results Analysis:

Deal with the biggest data problem: Because of the complexity of time, so when processing to the length of the number of the time is more than ten seconds, The same data can be processed, However, because there is too much data, only the results section can be displayed, and the previous parts cannot be displayed in the console.

Can handle the maximum number: Max to 99999999-999999999 can also be processed if the array range is in 999999999- 9999999999 An error message appears,

Exception in thread "main" Java.util.InputMismatchException:For input string: "9999999999"

At Java.util.Scanner.nextInt (Unknown Source)

At Java.util.Scanner.nextInt (Unknown Source)

At Sub-array changes . Zsz.main (zsz.java:19)

Knot Set Implementation:

Code part: Liu Hongyang; test part: Liu Di bo.

Integer Subarray max and add validation

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.