Topic Requirements Title: Returns the number of the largest subarray in an integer array. Requirements: Enter an array of shapes, with positive numbers in the array and negative values. One or more consecutive integers in an array make up a sub-array, each of which has a and. The array should be large enough to overflow. After overflow, add judgment. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n). Second, the design idea
Because the overflow will add a bit to the result of the last non-overflow, which causes the register memory to be placed in the overflow caused by the carry, it is judged to be added in front of this addition.
Third, the source code
#include <iostream.h>long int MaxSum2 (long int *a, int n) { long int nstart = a[n-1]; Long float Nall = a[n-1]; for (int i = n-2;i>=0;--i) { if (nstart<0) nstart = 0; Nstart + = A[i]; if (Nstart>=nall) { Nall = Nstart; if (nall>=1073741824) cout<< "Overflow!" << "\ t"; } } return Nall;} int main (void) {long int qian[100020];for (int i=0;i<100020;i++) {qian[i]=i;cout<<qian[i]<< "\ T";} Cout<<maxsum2 (qian,100020) <<endl;return 0;}
Iv. Results
Five, experience
Memory overflow is a serious problem, can not be ignored, especially in the bank, such as the huge amount of data, but also pay special attention. Increase overflow interpretation, can not fundamentally solve the problem. To fundamentally solve the problem, one to expand the scope, int32 to larger, if the maximum range, you have to customize the addition function, to calculate a larger number.
Development overflow problem of two-dimensional array maximal sub-arrays and pairs