Enter a set of integers to find the maximum value of the subarray.
Topics : returns the and of the largest subarray in an array of one-dimensional integers.
Requirements:
Enter a one-dimensional shape array with positive and negative numbers in the array.
One-dimensional arrays end-to-end, like one end-to-end tape.
One or more consecutive integers in an array make up a sub-array, each of which has a and.
The maximum value for the and of all sub-arrays.
?
10-9 8 7-5 3
?
I : 4 3 2) 1 0
?
Nall:3 3 7 15 15 16
?
Nstart:3-2 7 15 6 16
?
Nstart = Max (Arr[i], arr[i]+nstart);
Nall = Max (Nall, Nstart);
Nstart To record the result after each and the previous number, to the original Nall do the comparison, and Nall represents the maximum value of the Subarray and the array.
?
when it comes to the end, it may appear like 1,2 -3,4 This should be the case 4+1+2
Array was originally the , -3,4 Now let each number do an array header once, turning into a new array, for example 2 , -3,4,1 also have -3,4,1,2 and the 4,1,2 , -3 these three arrays, that is, the array is rotated once, the new three array will still be the maximum value of the sub-array, and then compared with the original array of sub-array and maximum value, come out the last sub-array and maximum value.
?
- Source code: /* To find the maximum value of the number of arrays in a neutron array. Cloud fly at the end of 2016.4.11 * *
- #include <iostream>
- using namespace std;
- int max (int x, int y)
- {
- ???? return (x > Y)? x:y;
- }
- To find the maximum value of a neutron array of an array
- int maxsum2_v (int arr[], int N)
- {
- ???? int i;
- ???? int Nall, nstart; //nall The maximum number of sub-arrays and
- ???? Nall = arr[n-1];
- ???? Nstart = arr[n-1];
- ???? for (i = n-2; I >= 0; i--)
- ???? {
- ?????????? Nstart = Max (Arr[i], arr[i]+nstart); a large number is retained after each sum of the previous numbers of the array.
- ?????????? Nall = Max (Nall, Nstart); //After keeping a larger number above, the original maximum and current and comparison, take a larger value
- ????}
- ???? return Nall;
- }
- void Main ()
- {
- ??? //n integer number, arr "" Array, anotherarr[100] for the computed rotation array, max Word Group and maximum value
- ??? int n=100,arr[100],anotherarr[100],max1=-100,max=-100;
- ??? cout<< " Please enter an integer number:";
- ??? cin>>n;
- ??? cout<< " Please enter an integer, each number is separated by a space:" <<endl;
- ??? for (int i=0;i<n;i++)
- ??? {
- ?????? cin>>arr[i];
- ?????? Anotherarr[i]=arr[i];
- ???}
- ??? Max=maxsum2_v (Arr,n); //Call the Maxsum2_v function
- ??? for (int i=1;i<n;i++) //Rotate the number in the array one at a time to create a new additional array
- ??? {
- ?????? for (int j=0;j<n;j++)
- ?????? {
- ????????? //Start rotation, an array of arr is required after the equals sign, and if used later, array confusion occurs after multiple loops
- ????????? anotherarr[j]=arr[(J+i)%n];
- ????????? Max1=maxsum2_v (Anotherarr,n); //Call the Maxsum2_v function
- ??????}
- ?????? if (Max1>max)
- ????????? MAX=MAX1;
- ???}
- ??? cout<< " sub-array and maximum:" <<Max<<endl;
- }
?
Summary :
This time the sub-array and the practice, the original array end-to-end into a chain, such as 1,2-3,4 This array, will appear 4,1,2 three sub-arrays and the largest, rather than from the back of the sub-array and the largest, so need to rotate the array inside, so that each value is rotated once the array header, Then we get another array, call the Subarray and the maximum value of the method to find the sub-array and the maximum value of each new array, and then compare, to obtain the maximum value of the sub-array.
You can not simply think of 1,2,-3,4 as 1,2,-3,4,1,2,-3 this array, and if the array is positive, the maximum value of the error will be increased.
Enter a set of integers to find the maximum value of the subarray. (after the array is end-to-end)