A topic and requirements:
Title: Returns the maximum number of sub-arrays in an integer array and
Requirement (new addition): ① if array a[0] ... A[j-1] Next to the end, allow A[i-1] ... A[n-1],a[0] ... A[J-1] The sum of the largest; ② returns the position of the largest subarray at the same time.
Two design ideas:
Our idea of this topic is to continue the thought of the previous one-dimensional array, only to make a little change, such as the number is five, 1,2,3,-1,-1. The array space we open is 10,
Save the five numbers again, that is, 1,2,3,-1,-1,1,2,3,-1,-1. Using loops is the number of cycles per five, such as the first loop to find the 1,2,3,-1,-1
Continuous maximum and, the second loop finds the maximum number of consecutive numbers in the 2,3,-1,-1,1, and so on.
Three experimental code
#include <iostream.h>#include<time.h>#include<stdlib.h>intMain () {intarry[ -]; intStart,end,i; LongSum,max; cout<<"Please input numbers:"<<Endl; for(i=0;i<Ten; i++) {cin>>Arry[i]; Arry[i+Ten]=Arry[i]; } Max=arry[0]; Start=0; End=0; for(intj=0;j<Ten; j + +) {sum=0; for(intk=j;k<Ten+j;k++) {sum=sum+Arry[k]; if(sum>max) {Max=sum; Start=j+1; End=k+1; } }} cout<<"MAX is"<<" "<<max<<Endl; cout<<"START:"<<" the"<<" "<<start<<"th"<<" "<<" Number"<<" "<<"END:"<<" the"<<" "<<end%Ten<<"th"<<" "<<" Number"<<Endl; cout<<"These numbers is:"; for(intC=0; c<end-start+1; C + +) {cout<<arry[start+c-1]<<" "; }}
Four experiments
Five experimental Thoughts
Our greatest impression of the experiment is that if a program to understand, add some conditions, and then make some changes, can be from the most primitive procedures to find a breakthrough,
This procedure our main idea and last time no difference, only made a small change on the success, classroom made out, so also very excited, of course, and the partner Smart
The head is not open to the relationship.
One-dimensional ring array