Homework two in class fourth week class

Source: Internet
Author: User

Title: Returns the number of the largest sub-arrays in an integer array. Requirements: 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)

My partner is Hou Tao, I am in charge of program analysis, code programming, he is mainly responsible for code review and Code test plan

Work Photo:

Design ideas: With a random number to generate a fixed number of n, which has a positive, for loop n times, the default maximum Subarray and zero, starting from a[0] to add each of the array to K, and then determine whether K is greater than the maximum subarray and maxsum, greater than the other maxsum=k At last, the judge whether K is greater than 0, if less than 0 let k=0; define an array as a "1000" so that you can handle 1000 numbers,Int32 value types represent signed integers with values between 2,147,483,648 and +2,147,483,647. When it is greater than this number, it is zeroed and can be implemented with the string type.

A problem occurred: when the array is too long or too large, it will appear zero, and the array length cannot exceed 250000.

Solution: Use string strings to solve large integer problems.

Code:

#include <iostream>
#include <cmath>
using namespace Std;
void Main ()
{
int a[25000],i,j,f,n;
int maxsum = 0;
int k = 0;
cout<< "Please enter the number of numbers in the array" <<endl;
cin>>n;
for (i=0;i<n;i++)
{
J=rand ()%2+1;
F=pow ( -1.0,J);
A[i]=f*rand ()%100000;
}

cout<<endl;
cout<< "Maximum sub-array is";

for (i=0;i<n;i++)
{

K + = A[i];
if (K > Maxsum)
Maxsum = k;

if (K < 0)
k = 0;

}

cout<<maxsum<<endl;
}

Summary: The array length cannot be too large, or the program will not run. Int32 has a certain range of values, when it is more than a string to solve. In addition, through this experiment I also understand the importance of cooperation.

Homework two in class fourth week class

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.