Pair Development 4----Maximum sub-array (large number overflow)

Source: Internet
Author: User

Pairs of Members: Van Dey, Zhao Yonghen

I. Title:

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

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

Three. Design ideas

In the last experiment, we set the number of arrays can be very well extended, so for the first request to deal with 1000 elements is easier to implement, for the second requirement, we mainly want to determine how big the largest number, that is, when the overflow problem, when the problem occurs when the program how to deal with, By looking at the data, we finally determine the maximum number of values that the program can handle, and then overflow through the output statement to prompt the array.

Four. Source code

#include <iostream.h> #include <time.h> #include <stdlib.h>int main () {Srand ((unsigned) time (NULL));              int A[1000];int m; M is the number of ints per group int *sum=new int[1000];cout<< "*********************************" <<endl;for (int i=0;i<1000;    i++) {int b;    B=rand ()%2;            Switch (b) {case 0:a[i]=rand () *350;        Break            Case 1:a[i]=-rand () *100;    break;}    cout<<a[i]<< "";         if ((i%10) ==9) cout<<endl; 10 outputs per line, newline}cout<< "*********************************" <<endl;int he=0;for (m=1;m<1001;m++) {int temp=0;for (int n=0;n<m;n++) {temp=temp+a[n];} for (int k=0;k<= (1000-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];}} if (temp>he) {he=temp;}} if (he>2100000000) {cout<< "Array overflow! "<<endl;} else{cout<< "The number of the largest sub-arrays is:" &LT;&LT;HE&LT;&LT;ENDL;} cout<< "*********************************" <<Endl;return 0;} 

Five. Running

Report:

Pair Development 4----Maximum sub-array (large number overflow)

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.