Returns the maximum number of sub-arrays in an integer array and

Source: Internet
Author: User

Title :

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

Requirements:

• Require that the program be able to handle up to three elements;

• What happens if each element is of type int32 and the sum of the sub-arrays is greater than the maximum range represented by the integral type;

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

• One or more consecutive integers in an array make up a sub-array, each of which has a and.

• The maximum value of the and of all sub-arrays is evaluated. Requires a time complexity of O (n).

Pair programming Requirements:

• Two pairs complete the programming task.

• One person is primarily responsible for program analysis, code programming.

• One person is responsible for code review and Code test plans.

• Publish a blog post about design ideas, problems that arise, possible solutions (multiple selection), source code, results, summary. (Until 3 months before 18:00 )

Expand:

the difference between int16,int32, andint64 in C #

(Note: There is no such term for these data types inJava , which is called in C # , with the short in javA , int , Long type one by one corresponds )

The Int16 value type represents a signed integer value between -32768 and +32768 .

The Int32 value type represents a signed integer value between -2,147,483,648 and +2,147,483,647 .

Int64 value type indicates a value between -9,223,372,036,854,775,808 and +9,223,372,036,854,775,807 The integer between.

The short keyword represents an integer data type that stores values based on the size and range shown in the following table.

Type

Range

Size

. NET Framework Types

Short

-32,768 to 32,767

Signed a -bit integer

System.Int16

--------------------------------------------------------------------------------------------------------------- -------------------------

The int keyword represents an integral type that stores values based on the size and range shown in the following table.

Type

Range

Size

. NET Framework Types

Int

-2,147,483,648 to 2,147,483,647

Signed three-bit integer

System.Int32

--------------------------------------------------------------------------------------------------------------- -------------------------

The long keyword represents an integral type that stores values based on the size and range shown in the following table.

Type

Range

Size

. NET Framework Types

Long

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Signed decimal integer

System.Int64

Design ideas

On the basis of the previous exercise, the length of the integer array is modified to be 100000, and then when the user inputs the number of random numbers, the input cansatisfy the ontology requirement to be able to handle each element.

Program source Code

Import Java.util.Random;

Import Java.util.Scanner;

public class Zuidazishuzu2 {

/**

* @param args

*/

public static void Main (string[] args) {

TODO auto-generated method stubs

int array[]=new int[100000];

int flag1=0;

int flag2=0;

int flagtemp1=0;

int flagtemp2=0;//record the starting and ending positions of the sub-series, respectively

int sum,sumtemp;//represents the and of the sub-series

Random r=new random ();

System.out.print ("Please enter the number of array elements:");

Scanner sc=new Scanner (system.in);

int Number=sc.nextint ();

System.out.println ("--------------------------------" +

"------------------------------------------");

System.out.print ("The resulting sequence of random numbers is:");

for (int i=0;i<number;i++) {

Array[i]=r.nextint ()%10;

System.out.print (array[i]+ "");

}

System.out.println ("");

SUM=ARRAY[0];

Sumtemp=sum;

for (int i=0;i<number;i++) {

if (sumtemp<=0) {

Sumtemp=0;

flagtemp1=i+1;

Flagtemp2=i;

}

SUMTEMP+=ARRAY[I+1];

flagtemp2++;

if (sumtemp>sum) {

Sum=sumtemp;

FLAG1=FLAGTEMP1;

FLAG2=FLAGTEMP2;

}

}

System.out.print ("The constituent elements of a sub-array are:");

for (int i=flag1;i<=flag2;i++)

System.out.print (array[i]+ "");

System.out.println (' \ n ' + ' sub-array and the maximum value is: "+sum);

}

}

Display results when the number of input array elements is 1000

Display results when the number of input array elements is 10000

Display results when the number of input array elements is 15000

display results when the number of input array elements is 99999

Summarize:

It can be seen that although the maximum number of sub- columns and values does not exceed the range represented by int, the resulting format changes: The sub-array composition element is displayed two times, and the resulting sequence of random numbers shows a change in position. Can be seen when the number of data to a certain extent, the program will not error but will occur a variety of error output, so in the future programming process, when the data is large, you should pay attention to the choice of data types of consciousness!

Possible solutions

1. If you know the scope of the data, you can choose the appropriate numeric type.

2. If big data exceeds the representation range of a numeric type, the integer data is converted to a string type for operation.

Zhang Huanlong is mainly responsible for program analysis, code programming.

Li rang responsible for code review and Code test plan.

The

Returns the maximum number of sub-arrays in an integer array and the

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.