Title: Returns the maximum number of sub-arrays in an integer array and
Requirements:Requires that the program must be able to handle 1000 elements; Each element is of type int32; Enter an array of shapes with positive and negative numbers in the array. 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. Requires a time complexity of O (n). In the previous topic, did not take into account the number of arrays of the upper limit and the problem of random numbers, that the program is not wrong, is perfect. And through the classroom validation of the last program, found that the array and more than a certain value will not be displayed, but the same, the program will not error, in the small program the consequences are not serious, but once the development of large software, it is very likely to cause irreparable errors and economic losses. The following limit is exceeded: The topic requires implementation code:
1#include <iostream>2#include <string>3#include <ctime>4 #defineN 10005 using namespacestd;6 7 intMaxsum (int*arr,intsize)8 {9 intMaxsum =0;Ten intb =0;//B is the and of the sub-array One A for(inti =0; i < size; i++) - { - if(b <0) theb = Arr[i];//when the sub-array and less than 0 o'clock, and regardless of how the following array is added, and certainly less than the sum of the following array, at this time, the B is re-assigned to the next element - Else -B + =Arr[i]; - if(Maxsum <b) +Maxsum =b; - } + returnmaxsum; A } at - intMain () - { - intArr[n]; -Srand (Time (0)); - for(intj =0; J < N; J + +) in { -ARR[J] = rand ()% +- -; tocout << Arr[j] <<" "; + - } thecout <<"the number of the largest sub-arrays is:"<< maxsum (arr, N) <<Endl; * return 0; $}
Sentiment: In the programming time is not only to complete the task goal, should consider the application scope of the program, and then optimize
Returns the maximum number of sub-arrays in an array of integers (2)