Introduction to Software engineering returns the and extension of a maximum sub-array

Source: Internet
Author: User

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

Related Article

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.