After-school experiment 4--returns the maximum number of sub-arrays in an array of integers and

Source: Internet
Author: User

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

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 learned some knowledge about the maximal value of array neutron array, although it seems to be simple in writing, actually the idea has some difficulty in understanding.

Although the experimental thinking is not our spontaneous thought out, but the teacher in the classroom to our inspiration and hints for reference, but we are in the writing is fully understood after digestion, the core idea: starting from the first number of arrays, judging not contain the current number of the previous number of all sub-array and the maximum value, and the maximum value of all sub-arrays that contain the previous number of the current number, and a comparison of the maximum values of the previous two, using the combination of the loop and the Max function to achieve the storage and updating of the maximum value, in such a cyclic mode, when a negative count is encountered, Determines whether the value of a negative number is added to the result, or only the maximum of the sub-array consisting of the number after negative numbers, through the previous maximum and negative values.

There is also the use of the Max function, I think this experiment exercise our logical thinking ability, I think it will be in my future program to play a role, I will transform it into my own knowledge to apply!

After-school experiment 4--returns the maximum number of sub-arrays in an array of integers and

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.