1. Title:
Returns the and of the largest sub-array in an integer array.
2. Requirements:
1) requires that the procedure be capable of handling 1000 elements;
2) Each element is of type Int32.
3. Design ideas:
When working with 1000 elements, let the user enter the desired array length themselves, see how long the runtime is processing, and then improve the problem in the source program.
When dealing with a large number overflow, the number of random functions generated is not too large, we try to let its final deposit into the array of elements close to the maximum range, and then the sum of the likelihood of a large number overflow situation.
4. Source code:
1#include <iostream>2#include <stdlib.h>3 using namespacestd;4#include <ctime>5#include <cstdlib>6 #defineMAX 1000007 intMain ()8 {9 intk,t=0, o=0;Ten intn=0, m=0; One intB[max]; A intA[max]; -cout<<"Please enter the number of integers in the array:"<<Endl; -Cin>>K; theSrand (Time (0)); - for(intI=0; i<k;i++) - { -A[i]=rand ()% -- -; +cout<<a[i]<<" "; - //cin>>a[i]; + } A at for(intL=1; l<k+1; l++) - { - for(i=0; i<k-l+1; i++) - { - for(intj=i;j<i+l;j++) - { in if(j>k-1) - { to Break; + } - Else them=m+A[j]; * } $b[n]=m;Panax Notoginsengm=0; -n=n+1; the } + } A intmax=b[0]; the for(i=1; i<n;i++) + { - if(max<B[i]) $max=B[i]; $ } - /*For (i=0;i<n;i++) - { the if (Max==b[i]) - {Wuyi m=i; the } - } Wu m=m+1; - int r=k; About for (int j=0;j<k;j++) $ { - if (m>=r&&r>=0) - { - M=m-r; A t=t+1; + r=r-1; the } - if (m==0&&r>0&&j!=k-1) $ { the m=m+r+1; the Break ; the } the if (m==0&&r==0&&t==k) - { in m=m+1; the Break ; the } About }*/ thecout<<Endl; thecout<<"The number of the largest sub-arrays in the array is:"<<max<<Endl; the /*cout<< "These numbers are:"; + m=m-1; - if (t==0) the {Bayi t=1; the } the For (i=m;i<m+t;i++) - { - cout<<a[i]<< ""; the } the For (i=0;i<n;i++) the cout<<b[i]<< "";*/ the return 0; -}
5. Results:
6. Experience:
The focus is on large number overflow resolution, the first time we achieve a large number overflow when the program unexpectedly closed, even if it can run without prompting the error. The solution to this point, we think of the array elements randomly generated when the re-generation, you can also judge the results, and actively suggest errors to solve. But we do not really solve the large number of overflow has no specific solution, this is our shortcomings, we must pay more attention to this point in the future.
Array length requirements and large number overflow for pair programming