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

Source: Internet
Author: User
Tags rand

One topic requirement (merger topic)

1 requires that the program must be able to handle 1000 elements; 2 Each element is of type Int32, 3 enters an array of shapes, and there are positive numbers in the array. 4 contiguous one or more integers in an array make up a sub-array, each of which has a and. 5 if the array a[0] ... A[j-1] next to each other, allowing a[i-1], ... A[n-1], a[0] ... A[J-1] and the largest. 6 returns the position of the largest sub-array at the same time. 7 The maximum value of the and of all sub-arrays is evaluated. Requires a time complexity of O (n). Two design ideas will be array and set to auto type variable, and the type will be adjusted to avoid the overflow problem of the output, and the array allows the end-to-end connection and the largest case, you can set the exit of the For Loop statement to twice times the array length and buffer accumulation does not exceed the array length to obtain a reliable value three source code
#include <iostream> #include <time.h> #include <conio.h> #define N 100000using namespace Std;void Randin (int intnum,int a[])//randomly generated intnum positive negative number output five columns {cout<< "Integer contents" <<endl;for (int i=0;i<intnum;i++) {a[i]= Rand ()-rand (); cout<<a[i];if (i%5==4) cout<<endl;elsecout<< ' \ t ';}} void Selmax (int intnum,int a[],auto &sum) {Auto buffer=0;//introduces a constant record that sums the sum of int count1=0;//buffer and the number of values int count2=0 The//for statement does the number of operations for (int j=0;j<=intnum;j++) {if (j==intnum) {j=0;} Buffer+=a[j];count1++;count2++;if (buffer<0)//if cumulative and less than 0,buffer reinitialized to 0{buffer=0;count1=0;} if (Sum<buffer)//sum always records the existence of the largest and {sum=buffer;} if (count1>intnum| | COUNT2&GT;INTNUM*2)//If the accumulated number count1 more than intnum jumps out of the loop, or the statement does more than twice times the number of operands {break;}} void Main () {int intnum;int a[n];int q=0;while (q==0) {Auto Sum=0;srand ((unsigned) time (NULL));cout<< "Please enter the number of integers:"; cin>>intnum; Randin (Intnum,a); Selmax (intnum,a,sum);cout<<endl;cout<<sum<<endl;cout<< "whether to continue the test (enter 0 to continue otherwise stop)";cin>> Q;system ("CLS");}}

Four procedures

Five knot pairs of photos

Six Experimental summary

This test further deepened the understanding of the array, because it is only the improvement of the previous program, the harvest is not so much, there will be more attempts later

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

Related Article

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.