The number of the largest sub-array in the array (2)

Source: Internet
Author: User

Title: Returns the maximum number of sub-arrays in an integer array and

Requirements:

Requires that the procedure be capable of handling 1000 elements;

Each element is of the int32 type;

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). Group: Chen Chen, Wang Ying I mainly responsible for the program analysis, code programming, in the process of clear division of Labor clearly also have the appropriate discussion summary, mutual exchange ideas to make a plan, encountered problems to discuss the solution. Work Photo: The idea: Using random numbers to generate a random array, input control array length, in turn, the sum of the number of arrays and if a negative number is updated so that the number of the next digit in the array continues just the operation, Starts making the largest subarray and is zero after the array is created and compared if smaller then the maximum number of sub-arrays is updated and the value is the larger of the comparison until the end of the loop. The elements and elements required to process more than 1000 elements are the Int32 type, where the number processing is larger when the processing array exceeds the int type, a bug occurs. Code:

#include <iostream>
#include <cmath>
using namespace Std;

void Main ()
{
int a[250009],b=0,s=0,i,l,n,c=2147483648;
cout<< "input array Length";
cin>>n;
for (i=0;i<n;i++)
{
L=rand ()%2+1;//produces a random array
const int Q=POW ( -1.0,L);
A[i]=q*rand ()%1000000+1;
}
for (i=0;i<n;i++)//Sub-array and maximum value
{
if (b<0)
B=a[i];
Else
B+=a[i];
if (s<b)
S=b;
}
cout<<endl;
cout<<s<<endl;
}

Problems that arise:

A bug occurs when the array length is more than 250000, and the value returns 0.

A bug occurs when the number in the array exceeds the Int32 range, and the value returns 0.

Solution: Take advantage of the string or char type.

Summarize:

Programming code is very important, programming specifications also have a lot to note, this test will appear a lot of different bugs, are not in the programming process, such as the definition of the type of int is we generally do not consider the value of the int type, when more than this range of errors will occur, Through this procedure I realized the importance of programming norms, in fact, the programming norms embodied in a lot of small details, we should pay attention to the habit, as the teacher said will give us a lot of benefits.

The number of the largest sub-array in the array (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.