Pair development, the first adjacent array to find the largest 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. Summary of the HARVEST

Or continued the main idea of the first experiment, but because at the beginning of our code did not achieve the location, this is a new addition, is the time complexity did not meet the requirements of the topic. Hotton the process of cooperative discussion is very enjoyable, I also like this very feeling of communication. The teacher is right, we should cooperate with others in the process of writing software should be more face-to-face communication.

Through so many times of the soft work industry, I feel more should learn how to cooperate with others, I need to exercise this aspect, before a person feels very good, now found that someone together boarding efficiency than their own twice times the time to do more things, efficiency really improved a lot!

Six, group photo

Pair development, the first adjacent array to find the largest 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.