Returns the maximum number of sub-arrays in an array of integers (2)

Source: Internet
Author: User

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)

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.