I. Topics and Requirements
Title: Returns the and of the largest sub-array in an integer array.
Requirements: Enter an integer array with positive 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. Requires a time complexity of O (n);
Second, design ideas
The program is divided into two parts:
- (1): Finds all possible sub-arrays in the array;
Start with the first number in the array, always ask for all the numbers that are contiguous with it, and save the array son, and then start with the second number, count the number of consecutive numbers and save the array, and so on, until the last number is calculated. An array of n data can produce a number of sub-arrays of n (n+1)/2
- (2) Each time you request and deposit an array, add one village to the new data according to the last data stored in the previous data
- (3) Compare the values in the array son[], and then output the maximum number as the
Third, the source code
1 //ketang4.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"iostream"6 using namespacestd;7 8 9 /*determining sum storage array elements*/Ten voidSon (intFather[],intSon[],intlength) One { A intAdd//Defining summation Variables - intCount=0;//count the number of array elements - for(intI=0; i<length;i++) the { -Add=0; - for(intj=0; j<length-i;j++) - { +add=add+father[i+j]; -son[count+j]=add; + } Acount=count+length-i; at } - } - - /*find the largest sub-array and*/ - voidMax (intSon[],intnum) - { in intmax=son[0]; - for(intI=0; i<num;i++) to { + if(max<Son[i]) - { themax=Son[i]; * } $ }Panax Notoginsengcout<<max<<Endl; - } the + /*Main function*/ A intMain () the { + intLength,num;//defines the length of the original array, sums the stored array length num -cout<<"Please enter the number of array elements:"; $Cin>>length; $ int* father=New int[Length];//defining the original array -num=length* (length+1)/2; - int* son=New int[Num];//Defining a sum storage array thecout<<"Please enter raw array data:"<<Endl; - for(intI=0; i<length;i++)Wuyi { theCin>>Father[i]; - } Wu Son (father,son,length); -cout<<"the number of the largest sub-arrays is:"; About Max (son,num); $ return 0; -}
Iv. Results of the experiment
Five, experimental analysis
This can be counted on is the first time trooped development, it was really not a few people in a piece to discuss the implementation of the problem, although there have been a group of experiments and other classes of homework, but, at most just beginning to discuss, then nothing, all things are doing themselves, think of what is what, will not take into account the thoughts of others. When I and Liang to do this problem, there are different ideas about this problem, but after thinking, decided to choose the above solution, although not to achieve the complexity of O (n), but still reached two people expected results. Although this program is very short, but two people in the completion process, there is communication and analysis, can know each other's ideas, can broaden their thinking, to achieve 1+1>2 effect.
Software engineering trooped Development--the output of the largest sub-array in an array