Title: Returns the maximum number of sub-arrays in an integer array and
Additional requirements: Let the array hand end connected, form a ring, allow a[n],a[0] ... This form. Returns the position of the largest subarray at the same time
Design idea: Break the ring, become a line, analyze the largest sub-array on this line, find the largest sub-array, you can find the maximum sub-array of the starting point and the end point, and then the end as a starting point, the starting point as the end point, the line is connected to the ring, in the search for the largest sub-array ( Finally, the two largest sub-arrays that have been found together form the largest subarray of this integer group ring, then output the starting and ending points of the record, and return the position of the largest sub-array.
Problem: Cannot enter more numbers, for example 1000
Possible solution: Define a larger range of shaped arrays, but I used Long,long long, usinged long,usinged long long.
Code:
Lu Guanghao 3/27#include<iostream> #define N 100using namespace std;void main () {int a[n], b[n][n];int length, I, j, W = 0, p = 0, q = 0, temp, m;cout << "input random Integer" << endl;for (length = 0;;) {cin >> a[length];length++;if (getchar () = = ' \ n ') {break;}} cout << "The length of this array is:" << length << endl;//for sub-array for (i = 0; i<length; i++)//two cycles, to exclude, to determine the maximum sub-array of each number of {m = I;w = 0;j = 0;while (J <= length-1) {w + = A[m];b[i][j] = w;m++;if (m>length-1) {m = 0;} j + +;}} temp = b[0][0];for (i = 0; i<length; i++)//determines the maximum subarray for each number, and finally gets the largest subarray of the entire integer group {for (j = 0; j<length; j + +) {if (B[I][J]&G T;TEMP) {temp = B[i][j];p = I;q = J;}}} cout << "Maximum subarray values are:" << temp << endl;cout << "The subscript of the elements in the maximum sub-array seats:" << endl;i = 0;while (i <= q {cout << P << " ";p ++;if (P >= length) {p = 0;} i++;} cout << Endl;}
Results:
Summary: This procedure thought is very important, from the ring to the line, from the ordinary to the special, must consider the situation also to be comprehensive.
To find the largest subarray of integers in an integer group (upgraded version)