The and of the maximal sub-arrays after one-dimensional sub-number is composed of rings

Source: Internet
Author: User

Group member: Ginger Wang Jibo design ideas (improved in the subsequent implementation process):
(1) The outer while loop control end (i=n) stops.
(2) Inner calculation: calculated as a 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 maximum number of sub-arrays. 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.
Source://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;

}

}
cout<<endl;
}
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 2n
cout<<"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]&GT;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];//stores the input array elements in this array, but the second time does not save the last one because it does not need to become a ring
}
for(intI=0;i<2*n-1; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
Max (n,a);
}

}

{
Mx

Screen:
Summary: First I think it is a good idea to store the array again as a ring, and then to have the idea of welcoming the wrong, when others point out to themselves wrong or can improve the place is not only their own procedures more perfect, personal ability and attitude towards learning also improved, The last is the ring is to trooped development task assignment feel overwhelmed I do not know how to coordinate the two people's task, can certainly have a pair of development to improve the efficiency of the method, otherwise how there will be those successful examples, need to improve in this respect.

The and of the maximal sub-arrays after one-dimensional sub-number is composed of rings

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.