Title: Returns the maximum number of sub-arrays in an integer array and
Extension requirements: 1, the program must be able to handle the number of elements, 2, each element is a int32 type, the sum of the sub-array is greater than the integer representation of the maximum range of what happens, 3, pair development.
Members: Guo Ting, Zhu Huimin.
Test program code: (Guo Ting program)
#include <iostream>#include<time.h>using namespacestd;#defineMax (a) (a) > (b)? ( A):(B))intMaxsum (intA[],intN) { inti; intMaxsofar =0; intMaxendinghere =0; for(i =0; I < n; i++) {Maxendinghere= max (Maxendinghere + a[i],0); Maxsofar=Max (Maxsofar, Maxendinghere); } returnMaxsofar;}intMain () {intN, I; cout<<"Enter the number of numbers that the array contains:"; CIN>>N; int*A; A=New int[n]; if(A = =NULL) {cout<<"Error! "; return 1; } srand ((unsigned) time (NULL)); for(i =0; I < n; i++) {A[i]= rand ()% $- -; cout<< A[i] <<" " ; } cout<<Endl; intmax=Maxsum (A, n); cout<<"the number of the largest sub-arrays is:"<< Max <<Endl; Delete[]a; return 0;}
Enter the 10,0000
wherein, according to the extension requirement 1, the program can handle 1000 elements. The maximum range that can be computed is 10,0000, and it takes about half a minute to calculate the 10,0000. In addition to the computed range, it is almost equal to the dead loop and the operation time is too long.
Solution: The solution to the direct response is to change int to longint or to a wider range of other types. Teacher Example Hospital Medical insurance system, the content of the query display year divided into monthly display, narrow the display, reduce the display content, to avoid content overflow. That is, by controlling the complexity of the time to control the range of operations. Personal feeling the teacher's method is quite skillful.
Pair development Author picture
Summary: Knot pair development exercise our ability to cooperate, Guo Ting procedures Compare standard, so on testing this program. By her program, I can see that I am really not very standard in programming, and my application scope is small, compared to the standard procedure is still a lot less. I feel that cooperation will enable me to learn their shortcomings, so that their procedures more standardized, not so "petty."
Introduction to Software engineering returns the and extension of a maximum sub-array