Loop one-dimensional array maximum sub-array

Source: Internet
Author: User

Pair of people: Zhang Shitong Liang

First, the topic

    • Returns the maximum number of sub-arrays in a one-dimensional array
    • Enter an array of shapes with positive and negative numbers in the array.
    • One or more consecutive integers in an array make up a sub-array, each of which has a and.
    • If the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1], a[0] ... A[J-1] and the largest.
    • Returns the position of the largest subarray at the same time.
    • The maximum value for the and of all sub-arrays.
    • Requires a time complexity of O (n)

Second, design ideas

1, on the last one-dimensional array of the largest subarray of the problem, to expand, continue to use a similar summation method

2. Determine the starting and ending positions of the largest subarray by defining the variable Start,finish

3. After each sub-array that relates to the array element A[i] is calculated, the value of a[i] is paid to a[length+i];

4, loop to the last array element A[length-1] When terminated, you can iterate through all the array elements

Third, the Code

1 //ketang6.cpp: Defines the entry point of the console application. 2 //3#include"stdafx.h"4#include"iostream"5 using namespacestd;6 7 /*determining sum storage array elements*/8 voidSon (intArr[],intLengthintStartintfinish)9 {Ten     intmax=arr[0]; One     intAdd=0;//Defining summation Variables A       for(intI=0; i<length;i++) -      { -Add=0; the          for(intj=i;j<length+i;j++) -         { -Add=add+Arr[j]; -             if(add>max) +             { -max=add; +start=i; AFinish=J; at             } -         } -Arr[length+i]=arr[i];//each time you put the calculated number to the last -      } -      if(finish>=length) -      { incout<<"the maximum sub-array start position is:"<<start+1<<Endl; -cout<<"the maximum sub-array termination position is:"<<finish-length+1<<Endl; to      } +      Else -      { thecout<<"the maximum sub-array start position is:"<<start+1<<Endl; *cout<<"the maximum sub-array termination position is:"<<finish+1<<Endl; $      }Panax Notoginsengcout<<"the maximum number of sub-arrays is:"<<Endl; -       for(intm=start;m<=finish;m++) the      { +cout<<arr[m]<<" "; A      } thecout<<Endl; +cout<<"the number of the largest sub-arrays is:"<<Endl; -cout<<max<<Endl; $ } $  - /*Main function*/ - intMain () the { -     intNum,length,start,finish;//defines the length of the original array, sums the stored array length numWuyistart=0; theFinish=0; -cout<<"Please enter the number of array elements:"; WuCin>>length; -num=2*length; About     int* arr=New int[Num];//defining the original array $cout<<"Please enter array data"<<Endl; -      for(intI=0; i<length;i++) -     {     -Cin>>Arr[i]; A     } +cout<<Endl; the Son (arr,length,start,finish); -     Delete[]arr; $     return 0; the}

Iv. Results of operation

V. Analysis and Summary

This is the last one-dimensional array expansion, the number of code is not large, so in this code implementation process, mainly a discussion of the process, we mainly discuss the implementation of the loop array method and the way to return the position of thinking. I think in this pair development, play out the advantages of cooperation, the idea came particularly fast, probably the idea of a collision of Sparks, and soon solved the problem. Of course, it's not accidental.

Although this time the code is not long, but still encountered the problem in solving the time complexity O (n), the return position is not correct, so took another approach, the time complexity of O (N2), although not to achieve time complexity, but the process of solving the problem is very important, this is the two of us can insist on the fundamental cooperation

Pair development, to two people to provide a positive condition, can also give both sides a certain pressure, can stimulate us to pay a great deal of enthusiasm into the design and development, in this process, two people may have contradictions, but can resolve contradictions together, thinking of the touch will appear new ideas. trooped development, value is the cooperation of two people, in this, I am responsible for the preparation, Liang responsible for the review, which for us two people are helpful, can be our level to get a common promotion.

Vi. Work Photos

Loop one-dimensional array maximum sub-array

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.