-The subject of the experiment:
n returns the and of the largest sub-array in an integer array.
second, the experimental requirements:n requires that the program be able to handle 1000 elements; n Each element is of type int32; n inputs an array of shapes, and there are positive and negative numbers in the array. A contiguous integer or integers in an n array make up a sub-array, each of which has a and. N to find the maximum value of the and of all sub-arrays. Requires a time complexity of O (n).
Third, the source code:
ImportJava.util.Random; Public classMax { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub intn=1000; intc[]=New int[n]; intd[]=New int[n]; intA=0; intMax=0; intB=0; LongMax1=0; LongB1=0; DoubleMax2=0; DoubleB2=0; intMax3=0; intB3=0; intm1=2147483600; intm2=2147483647; Random Rand=NewRandom (); //randomly generated array c[n] for(inti=0;i<n;i++) {a=rand.nextint (2); if(a==0) {C[i]=(int) (M1+math.random () * (m2-m1+1)); } Else{C[i]=-(int) (M1+math.random () * (m2-m1+1)); } } //randomly generated array d[n] for(inti=0;i<n;i++) {a=rand.nextint (2); if(a==0) {D[i]=(int) (Math.random () * (100)); } Else{D[i]=-(int) (Math.random () * (100)); } } //calculate the result of a int32 type overflowB=c[0]; MAX=C; for(inti=1;i<n;i++) { if(b<0) {b=C[i]; if(max<b) {MAX=b; } } Else { if(c[i]<=0) { if(max<b) {MAX=b; } b=b+C[i]; } Else{b=b+B[i]; if(max<b) {MAX=b; } } } } //by converting into a double type calculationB2=c[0]; MAX2=B2; for(inti=1;i<n;i++) { if(b2<0) {B2=C[i]; if(max2<B2) {MAX2=B2; } } Else { if(c[i]<=0) { if(max2<B2) {MAX2=B2; } B2=b2+C[i]; } Else{B2=b2+C[i]; if(max2<B2) {MAX2=B2; } } } } //by converting it into a long type calculationB1=long.valueof (c[0]); MAX1=B1; for(inti=1;i<n;i++) { if(b1<0) {B1=long.valueof (C[i]); if(max1<B1) {MAX1=B1; } } Else { if(c[i]<=0) { if(max1<B1) {MAX1=B1; } B1=b1+long.valueof (C[i]); } Else{B1=b1+long.valueof (C[i]); if(max1<B1) {MAX1=B1; } } } } //The average program handles the results of 1000 elementsB3=d[0]; MAX3=B3; for(inti=1;i<n;i++) { if(b3<0) {B3=D[i]; if(max3<B3) {MAX3=B3; } } Else { if(d[i]<=0) { if(max3<B3) {MAX3=B3; } B3=b3+D[i]; } Else{B3=b3+D[i]; if(max3<B3) {MAX3=B3; } }}} System.out.println ("The test int31 array is:"); for(inti=0;i<n;i++) {System.out.print (C[i]+" "); if((i+1)%100==0) {System.out.println (); }} System.out.println ("The test program processes 1000 elements in an array of:"); for(inti=0;i<n;i++) {System.out.print (D[i]+" "); if((i+1)%100==0) {System.out.println (); }} System.out.println (); System.out.println ("Array elements are 1000, 100 are randomly taken:"); System.out.println ("Maximum array and for:" +MAX3); System.out.println (); System.out.println ("Int32 type Overflow test:"); System.out.println ("Overflow Displays the result of the maximum array and is:" +MAX); System.out.println ("After resolving the result, the maximum array and is: (1) to find the maximum subarray and double type:" +MAX2); System.out.println ("(2) to find the maximum subarray and the time with a long type:" +MAX1); } }
Iv. operation of the program:
V. Emerging Problems and Solutions
Problem: The result shows the maximum value of the Int32 type when the resulting result exceeds the maximum range of Int32.
Workaround: Convert to double or long for array addition.
Vi. Summary
The test array can test the maximum number of elements is not completed, when the test array element is 5000000, the program ran for more than 40 minutes did not appear results.
This pair development, their biggest harvest is to learn to communicate with peers, learn to listen to other people's opinions, in the opinion does not match, to adjust the good mentality.
Seven, teammates
Returns the and of the largest sub-array in an integer array.