Pair development _ Finding the largest subarray of one-dimensional array and judging overflow

Source: Internet
Author: User

Topic

Find the largest subarray of one-dimensional array and

Requirements: 1000 numbers or more, integers

Two. Design ideas

The maximum subarray is well found, but the sub-array and overflow problem is difficult to solve.

After constant thinking, I came to the conclusion that:

Overflow is a mistake caused by the insufficiency of the programmer's design. So designers should be very vigilant in coding, prevent overflow, this is the most essential solution to the problem. In this case, we set the maximum number of elements, as well as other input requirements, as far as possible to avoid the occurrence of overflow. The addition of 1000 numbers can be achieved, but not to take 1000 the largest number added, so that the calculation is meaningless, will not appear in our life learning, this is a scientific issue, to the great God to solve it.

Program Source code:

#include <iostream.h> #include <stdlib.h> #define Max_len 2000int Main () {int i,j;    int Len,max;   int value_max=2147483580;    int sum=0;//summation variable do{cout<< "Please enter the number of array elements (less than):"; cin>>len;} while ((Len>max_len) | | | len<=0);    int* array=new int[len];//Open array space for    (i=0;i<len;i++)        Array[i]=rand ()%1000;    cout<<endl;    MAX=ARRAY[0];    for (i=0;i<len;i++)    {sum=0;        for (j=i;j<len;j++)        {            sum=sum+array[j];            if (Sum>max&&sum<value_max)                   max=sum;            else if (Sum>value_max) break                ;        }} cout<< "The number of the largest sub-arrays is:" <<max<<endl;    delete []array;    return 0;}

Program run:

This pair of photos:

Too much food, or a low-key point.

Experimental thoughts:

  

Source and Csdn, I still do not understand.

Pair Development _ Find the largest subarray of one-dimensional array and determine overflow

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.