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