Development overflow problem of two-dimensional array maximal sub-arrays and pairs

Source: Internet
Author: User

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

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.