Returns the largest sum of the numbers in a one-dimensional array ring

Source: Internet
Author: User

Project members: Kangzhong, Wang Zhaobo.

1. Design ideas:

(1) The outer while loop control end (i=n) stops.

(2) Inner calculation: According to the method of non-circular array, whenever the outer layer I is incremented by 1, the array is updated, the number of i-1 becomes the nth number of the array,

The number of I becomes the first number of the array and the solution of the largest subarray. and records the starting position coordinates of the largest subarray into an array,

The maximum and amount of each calculation is also stored in an array, and the last comparison gets the coordinates of the first position of the array with the maximum and the maximum number of children.

2. Implementation code:

//2015/4/4#include <iostream>using namespacestd;voidMaxintLint*A)//when the element is not all negative{    intsum=0, J,p;  for(intm=0; m<l;m++)//the first element of control is that one .{p=0;  for(inti=m;i<l+m;i++)//the array size of the control comparison is always L        {            if(p<0) {p=A[i]; }            Else{p=p+A[i]; }          if(sum<p) {sum=p; J=i;//records the coordinates of the last element of the current largest sub-array} }} cout<<"the number of the largest sub-arrays is:"<<sum<<Endl; cout<<"the element position of the maximum sub-array is (from backward to forward output):"<<Endl; P=0, sum=0; intFlag;  for(inti=j-l+1; i<=j;i++)    {        if(a[i]>0) {flag=i;  Break; }    }     for(inti=j;i>=flag;i--)//Looking forward from the last element a search range is L The result is the subscript of all elements of the largest subarray from J to j-l+1 range    {        if(p<0) {p=A[i]; }        Else{p=p+A[i]; cout<<i%l+1<<" "; }        if(sum<p) {sum=p; }        }        }voidMain () {intN,count=0, MX; cout<<"Please enter the length of the array:"; CIN>>N; int*a=New int[2*n];//request a dynamic array array with a length of 2ncout<<"Please enter an array element:";  for(intI=0; i<n;i++) {cin>>A[i]; if(a[i]<0) {Count++; }    }        if(count==n)//when the array elements are all negative, simply compare the size of the individual elements and write down the coordinates{mx=a[0]; intC=0;  for(intI=1; i<n;i++)        {            if(a[i]>mx) {mx=A[i]; C=i; }} cout<<"the number of the largest sub-arrays is:"<<mx<<Endl; cout<<"the locations of the maximum and sub-arrays are:"<<c+1<<Endl; }    Else    {         for(inti=n;i<2*n-1; i++) {A[i]=A[I-N];//The input array element is stored in this array, but the second time does not exist because it does not need to become a ring        }       for(intI=0;i<2*n-1; i++) {cout<<a[i]<<" ";    } max (n,a); }}

3. Results:

Summary of Experience:

In this pair development process, I found that although I can find some ideas as soon as possible, but my ability to transform the lack of some, not a good idea into the code, it also makes me realize the strength of the team, the strength of the team can make the process faster (although I also know that it needs to work well with each other). Everyone has their own good side, I have heard a word, "there is no perfect person in the world, but the world has the perfect team." "So I will try to develop my strengths (though I haven't found them) and play my part in the team."

Returns the largest sum of the numbers in a one-dimensional array ring

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.