Returns the maximum number of sub-arrays in an integer array and

Source: Internet
Author: User

First: topic content and design ideas

1. Title:

Returns the number of the largest array in an integer array and

2. Requirements:

(1) Enter an array of integers with positive and negative numbers in the array.

(2) One or more consecutive integers in the array make up a sub-array, each of which has a and.

(3) The maximum value of the and of all sub-arrays is evaluated. The time complexity of the ball is O (n).

3. Design ideas:

(1) using the while loop input each integer, using the GetChar () function to determine if the while loop is over, when the user presses the return key, that is, getchar () = = ' \ n ', jump out while loop;

(2) record the number of cycles, that is, the length of the array;

(3) Call the function compare (int group[],int Length) to judge the input array, and return the maximum number of sub-arrays;

(4) In the Compare function, the maximum subarray is judged by recursive thinking.

4. Pair development partners:

Name: Cheng Simin

Blog name: Bright Future's thoughts

Blog Address link: http://home.cnblogs.com/u/pengchengwanli/

Two: Concrete realization

1. Experiment Code:

Returns the #include<iostream> of the largest subarray in an integer array #define N 100using namespace std;int Compare (int group[], int Length) {int Maxsum[n][2];//maxsum[n][0] indicates that the largest subarray and//maxsum[n][1 in the number of previous N-1) represent the largest sub-array of the number of previous N-1 and the maximum value of Gadi N, compared to the nth number maxsum[0][0] = MAXSUM[0][1] = group[0];for (int i = 1; i < Length; i++) {maxsum[i][0] = __max (maxsum[i-1][0], maxsum[i-1][1]); MAXSUM[I][1] = __max (maxsum[i-1][1] + group[i], group[i]);} Return __max (Maxsum[length-1][0], maxsum[length-1][1]);} int main () {int number[n];//integer array int length;//array length cout << "Please enter an integer array:" << endl;cin >> number[0];length = 1;while (GetChar ()! = ' \ n ') {cin >> number[length++];} cout << "The largest sub-array in the array and is:"; cout << Compare (number, length) << Endl;return 0;}

2. Running Results

Three: summary

This experiment to say difficult is not difficult, mainly is the algorithm is not clear, later after consulting related books, or solve the problem.

Project Plan Summary:

Date \ Task Lectures Writing Programs Access information Total Day
Monday 2 1 3
Tuesday
Wednesday 1 1
Thursday 2 2
Friday 1 1
Saturday 2 2
Sunday
Weekly totals 4 3 2

9

Time log:

Date Start time End time Interrupt Time Quiet time Activities Note
3/21 14:00 15:50 10 100 Lectures Software
19:30 20:40 10 60 Access information Read the data structure textbook
3/22
3/23 19:20 20:20 60 Access information Read the data structure textbook
14:20 15:30 10 60 Writing Programs Preparation of the Tuesday program
3/24 14:00 15:50 10 100 Lectures Software
3.2T 19:30 20:40 10 60 Writing Programs Preparation of the Tuesday program
3/26 9:20 11:40 10 130 Writing Programs Preparation of the Thursday program
13:20 14:20 60 Write a blog Write a blog
3/27

Bug log:

Date Number Introduction phase Exclusion phase Repair Time & Problem description
3/21 1
3/22 2
3/23 3 Coding Enquiry Information Write the program, do not understand the GetChar () function, after half an hour to solve the problem
3/24 4
3/25 5 Coding Debugging Write programs, debug, solve recursion problems, program completion
3/26 6
3/27 7

Returns the maximum number of sub-arrays in an integer array 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.