Design ideas:
From the first number of arrays to loop, and then add back, if the resulting and larger than the previous and large resets the maximum value is current, if the resulting and negative, plus will affect the maximum value, so reset to 0, after the cycle if it is 0, then all the numbers are negative, just find the maximum value can be
Source:
#include <iostream>using namespacestd; #include<vector>intMain () {intI,len; intmax=0, sum=0; cout<<"Please enter the length of the array;"; CIN>>Len; int*array =New int[Len]; cout<<"Please enter the contents of the array:"<<Endl; for(i=0; i<len;i++) {cin>>Array[i]; } for(i=0; i<len;i++) {sum= Sum +Array[i]; if(sum<0) {sum=0; } if(sum>max) {Max=sum; } } if(max = =0) {Max= array[0]; for(i=1; i<len;i++) { if(array[i]>max) {Max=Array[i]; } }} cout<<"the maximum number of sub-arrays and"<<max<<Endl; Delete[] array;}
Results:
The and of the largest sub-arrays in an integer array