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

Source: Internet
Author: User

1. Title:

Returns the and of the largest sub-array in an integer array.

2. Requirements:

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).

3. Development process and ideas:

My partner is Wu, just at the beginning of our two think are relatively simple and more unified, just want to use a for loop to output each group of and, in a comparison of the function to find the maximum value. But our idea was too simple, and then, after the discussion between us, we decided to use a nested loop to achieve the sum of each set of values. In the comparison function we also have a disagreement, at first he wanted to put the comparison function in the loop, the value of the temp is also placed inside each time, the result is always output a single value. Finally, we define the initial value of temp in the front of the main function, and realize the normal operation of the program.

4. Program code:

#include <iostream.h>

int main ()

{

int a[10];

int m; M is the number of each group

int *sum=new int[10];
cout<< "Please enter array:" <<endl;

cout<< "*********************************" <<endl;

for (int i=0;i<10;i++)

{

cin>>a[i];

}

cout<< "*********************************" <<endl;

cout<< "Please enter the number of each group:" <<endl;

cin>>m;

cout<< "*********************************" <<endl;

int temp=0;

for (int n=0;n<m;n++)

{

Temp=temp+a[n];

}

for (int k=0;k<= (10-M); k++)

{

sum[k]=0;

for (int j=k;j< (k+m); j + +)//a[k] is the first number of each group {

SUM[K]=SUM[K]+A[J];

}

if (sum[k]>temp)

{

TEMP=SUM[K];

}

}

cout<< "Max and as:" <<temp<<endl;

cout<< "*********************************" <<endl;

return 0;

}

  

5. Operation Result:

6. Impressions:

Two human knot group development, in the thinking can learn from each other, a person to discuss also can speed up the programming speed. The most let me feel deep is some time oneself a person programming not particularly big motivation and enthusiasm, encounter a difficulty always can not be adjusted out words easily lose confidence and give up. Two people together to complete this project, the mobilization of my enthusiasm, always feel can not work in the collective, two people together to solve a problem, thinking will be very active, very attentive. So the program can be completed in a very small amount of time.

7. Work Photo:

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

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.