Software Engineering course Assignment (iv)--Returns the maximum number of sub-arrays in an integer array

Source: Internet
Author: User

Partner Links: http://www.cnblogs.com/haoying1994/

First, design ideas

This experiment requires the input of a positive and negative number mixed integer array, the length is not unlimited, in this array of all sub-arrays found and the largest array, and the corresponding array and the time complexity of O (n). We discussed a variety of solutions in the classroom, which will be shown in the following possible solutions, after listening to the students ' thinking and the teacher's explanation, we finally chose the teacher in class described in the relatively simple ideas. As follows:

In the input array, use the for Infinite loop plus if to determine the cutoff, until the return key is triggered, the array is recorded in the array, the length of the arrays is recorded in the lengths.

Judging the maximum, starting from the first number of the array, judging the maximum value of all sub-arrays that do not contain the previous number of the current number, and the maximum value of all the sub-arrays that contain the previous number of the current number, and the comparison of the maximum values of the previous two, using the combination of the loop and Max functions to store and update the In such a cyclic mode, when a negative number is encountered, the maximum value of the negative number is determined by the change in the maximum value of the previous digit and the negative, or the largest of the subarray consisting only of numbers after negative numbers. This achieves all the requirements in the topic.

In the classroom, we feel that the teachers and students of the idea is very ingenious, on the basis of a complete understanding, through the guidance of ideas so that we write the procedure.

Ii. problems that arise

Of course, the light is not enough, in writing programs and debugging when there are some problems.

For example, when we entered the array, we chose to use the function GetChar () at the beginning of the input, the results found that because of the char type and int type conversion problem, so that the output is ASC code, so we directly input into the array, solve the problem.

Also in the calculation of the maximum value of the sub-array, in the process of maximum substitution ignores the maximum value of the previous array, causing the output to not meet the requirements, we found this problem in the process of single-step calculation on paper, adjusted the order of saving the maximum value, the final debugging success.

Iii. Possible Solutions

In the course of our discussion, there were altogether three solutions:

1. When the time complexity of O (n) is not considered, the and of all the sub-arrays in the array are computed, and each is compared in the array to find the maximum and.

2. A single consideration, find the maximum value of the entire array, and then 22 adjacent to consider, in three adjacent to consider, until the complete number of groups to traverse, find the largest subarray of the and.

3. The third option is what the teacher says in class, starting with the first number of arrays, judging the maximum of all sub-arrays that do not contain the previous number of the current number, and the maximum of all sub-arrays that contain the previous number of the current number, and a comparison of the previous maximum values.

Iv. Source Code

//3.21 Cheng qiqin Hao Ying//returns the maximum number of sub-arrays in an integer array and#include<iostream>using namespacestd;intMain () {intarray[ -]; intI,length=0;//to record the number of leaders    intSumofarray;//Sumofarray is used to hold the maximum value of the number of sub-arrays containing the current numbers    intsum=0;//sum is used to hold the maximum of the sum of all the sub-arrays that do not contain the current number     for(length=0;;) {cin>>Array[length]; Length++; if(GetChar () = ='\ n')        {             Break; }} cout<<"The length of this array is:"<<length<<Endl; Sumofarray=array[0];  for(i=1; i<length;i++) {sum=Max (Sum,sumofarray); Sumofarray=max (sumofarray+Array[i],array[i]); } Sumofarray=max (Sum,sumofarray);//Sumofarray maximum value used to hold the and of all sub-arrayscout<<"The maximum value of the sum of the sub-arrays of this integer array is"<<sumOfArray<<Endl; return 0;}

V. Results


Vi. Summary

Through this experiment, I feel that my logical thinking has been improved, and harvested on the array of neutron array of the maximum value of knowledge, I think our source code, although it looks very short, very simple, in fact, the key code in the understanding of the existence of a certain degree of difficulty.

Because there is O (n), so that we chose the teacher in the classroom to remind us, so we wrote according to the teacher's idea of the program: starting from the first number of arrays, to determine the number of all sub-arrays not containing the current number of the maximum value, and contains the current number of the previous number of all sub-array and the maximum value and the maximum value of the previous two comparisons, using the combination of the loop and the Max function to achieve the maximum value of storage and update, in such a circular mode, when encountering negative numbers, the maximum value after the previous number and the maximum value of the change to determine whether the value of negative values are added to the results, or the maximum value of the sub-array consisting only of the numbers after the negative number is considered. So although this idea is not entirely on our own, but our understanding of the idea is profound, all of us still harvest a lot of knowledge.

I believe that in the future process development, I will learn from today's ideas, many ideas to help me solve a lot of problems, play its role, in the back of the experiment I will continue to work hard!

Software Engineering course Assignment (iv)--Returns the maximum number of sub-arrays in an integer 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.