1. Title.
Title: Returns the and of the largest sub-array in an integer array.
Requirements:
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.
If the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1], a[0] ... A[J-1] and the largest.
Returns the position of the largest subarray at the same time.
The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).
2. Design ideas.
The randomly generated number is continuously deposited in the array two times, and in the first, each of its sub-arrays is calculated and compared. The maximum maximum number of sub-arrays is calculated.
3. Code.
#include <iostream>using namespacestd; #include<ctime>#include<cstdlib>#defineMAX 10000intMain () {intk,a[max],b[max],m,n=0, P,c,d; cout<<"Please enter the number of arrays:"<<Endl; CIN>>K; Srand (Time (0)); cout<<Endl; cout<<"the numbers in the array are:"<<Endl; for(intI=0; i<k;i++) {A[i+k]=a[i]=rand ()% -- -; cout<<a[i]<<" "; } cout<<Endl; cout<<Endl; for(i=0; i<k;i++) {m=A[i]; B[n]=m; N++; for(intj=i+1; j<i+k;j++) {m=m+A[j]; B[n]=m; N++; } } intmax=b[0]; for(i=1; i<n;i++) { if(max<B[i]) {Max=B[i]; P=i+1; } } //p=p+1;cout<<"the maximum number of sub-arrays is:"; cout<<max<<Endl; cout<<Endl; C=p/K; D=p%K; cout<<"the maximum number of sub-arrays is:"; for(i=c;i<c+d;i++) cout<<a[i]<<" "; cout<<Endl; return 0;}
4. Run the results.
5. Experience.
Pair development can speed up the programming speed, and complete the experiment programming faster and better.
6. Photos.
Returns the and of the largest subarray in an integer array (array-to-tail connections)