One
Title Requirements:
Returns the maximum number of sub-arrays in a one-dimensional array
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.
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.
Returns the position of the largest subarray at the same time.
The maximum value for the and of all sub-arrays.
Requires a time complexity of O (n)
Two
Design ideas: (pair thinking and reference to other students)
Consider that the array needs to be connected to the end, take the length of the array to increase twice times, and the copied array is written in the back of the last number, so that in the process of the loop does not need to adopt a new algorithm, as long as the original code slightly modified, plus a decision to make the largest array of the length of the original array.
Three, code
#include <iostream.h>#include<stdlib.h>intMain () {intNum,length,start,finish; Start=0; Finish=0; cout<<"Please enter the number of array elements:"; CIN>>length; Num=2*length;//allocate twice-fold array space to facilitate next comparison int* array=New int[Num];//Allocating array Space for(intI=0; i<length;i++)//array of positive and negative alternating occurrences { if(i%2==0) Array[i]=rand ()% +; ElseArray[i]=0-(rand ()% +); } cout<<"the array is:"; for(i=0; i<length;i++) {cout<<Array[i]<<" "; } cout<<Endl; //The following code references with other classmates as well as the degree Niang intmax=array[0]; intsum=0;//Defining summation Variables for(i=0; i<length;i++) {sum=0; for(intj=i;j<length+i;j++) {sum=sum+Array[j]; if(sum>max) {Max=sum; Start=i; Finish=J; }} array[length+i]=array[i];//each time you put the calculated number to the last } if(finish>=length) {cout<<"the maximum sub-array start position is:"<<start+1<<Endl; cout<<"the maximum sub-array termination position is:"<<finish-length+1<<Endl; } Else{cout<<"the maximum sub-array start position is:"<<start+1<<Endl; cout<<"the maximum sub-array termination position is:"<<finish+1<<Endl; } cout<<"the maximum number of sub-arrays is:"<<Endl; for(intm=start;m<=finish;m++) {cout<<Array[m]<<" "; } cout<<Endl; cout<<"the number of the largest sub-arrays is:"<<Endl; cout<<max<<Endl; delete []array; return 0; }
Four, the experiment
Five, knot pair development experience
This experiment obviously more than I paid teammates, but this also reflects the charm of development, not to say that everyone must pay the equivalent, so that everyone in the state as free as possible sometimes will be more effective.
Pair development--the maximum value of the loop array