Pairs of Members: Van Dey, Zhao Yonghen
I. Title:
Returns the and of the largest sub-array in an integer array.
Two. Requirements:
Requires that the procedure be capable of handling 1000 elements;
Each element is of the int32 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 for the and of all sub-arrays.
Three. Design ideas
In the last experiment, we set the number of arrays can be very well extended, so for the first request to deal with 1000 elements is easier to implement, for the second requirement, we mainly want to determine how big the largest number, that is, when the overflow problem, when the problem occurs when the program how to deal with, By looking at the data, we finally determine the maximum number of values that the program can handle, and then overflow through the output statement to prompt the array.
Four. Source code
#include <iostream.h> #include <time.h> #include <stdlib.h>int main () {Srand ((unsigned) time (NULL)); int A[1000];int m; M is the number of ints per group int *sum=new int[1000];cout<< "*********************************" <<endl;for (int i=0;i<1000; i++) {int b; B=rand ()%2; Switch (b) {case 0:a[i]=rand () *350; Break Case 1:a[i]=-rand () *100; break;} cout<<a[i]<< ""; if ((i%10) ==9) cout<<endl; 10 outputs per line, newline}cout<< "*********************************" <<endl;int he=0;for (m=1;m<1001;m++) {int temp=0;for (int n=0;n<m;n++) {temp=temp+a[n];} for (int k=0;k<= (1000-M); k++) {sum[k]=0;for (int j=k;j< (k+m); j + +)//a[k] is the first number of each group {SUM[K]=SUM[K]+A[J];} if (sum[k]>temp) {temp=sum[k];}} if (temp>he) {he=temp;}} if (he>2100000000) {cout<< "Array overflow! "<<endl;} else{cout<< "The number of the largest sub-arrays is:" <<HE<<ENDL;} cout<< "*********************************" <<Endl;return 0;}
Five. Running
Report:
Pair Development 4----Maximum sub-array (large number overflow)