First, the topic requirements
1.1 Enter an array of integers with positive numbers in the array and negative values. 1.2 contiguous one or more integers in an array make up a sub-array, each of which has a and. 1.3 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. 1.4 Returns the position of the largest sub-array at the same time. 1.5 The maximum value of the and of all sub-arrays is evaluated. Requires a time complexity of O (n).
Second, the design idea
Third, the 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>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, the operation
V. Summary of the Experiment
Compilation errors that occurred during the experiment:
Error C3533: "Auto &": The parameter cannot be a type that contains "auto".
In the vc++6.0 can be compiled through, but in the vs2013 of the above error message, on the internet did not find a good solution, and finally had to honestly change auto to int, compiled through.
March 27 Friday Classroom Exercise: Pair development----Returns the maximum number of sub-arrays in an integer array and three