Array 2--array end-to-end, to find the largest sub-array

Source: Internet
Author: User

Pair development My teammate Wang Chunfan, study number 20132902 blog address http://www.cnblogs.com/-beauTiFul/

Design ideas:
Input array 1-4-2 7

A[0]=sz[0];1
A[1]=max (A[0]+sz[1],sz[1]);-3
A[2]=max (a[1]+sz[2],sz[2]);-2
A[3]=max (A[2]+sz[3],sz[3]); 7

Max (A[0],a[1]...a[n])

Also use the idea of the array 1, because it can be connected to the end, it is converted to the {1,-4,-2,7}{-4,-2,7,1}{-2,7,1,-4}{7,1,-4,-2} The maximum number of arrays, and then the sum of the sub-arrays.

#include <iostream>#include<algorithm>#include<cstdio>#include<vector>using namespacestd;intMain () {inti,j, SZGS,SZGS1, maxsum; intsz[ -], a[ -];//Maximum Arraycout <<"Please enter the number of array elements:"; CIN>>Szgs; cout<<"Please enter an array element:";  for(i =0; i < Szgs; i++) {cin>>Sz[i]; } a[0] = Maxsum = sz[0]; SZGS1=Szgs; if(szgs>0)    {         for(j =0; J < Szgs; J + +)        {             for(i = j +1; i < SZGS1; i++) {A[i]= Max (A[i-1] +Sz[i], sz[i]); Maxsum=Max (Maxsum, A[i]); } SZ[SZGS1]= sz[szgs1-Szgs]; A[j+1] = sz[szgs1-szgs+1]; SZGS1=szgs1+1; }} cout<<"the number of the largest sub-arrays is:"<< Maxsum <<Endl;}


Array 2--array end-to-end, 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.