1#include <iostream>2#include <ctime>3 #defineN 2000//macro Definition4 using namespacestd;5 voidMain ()6 7 {8 intA[n];//array of integers9 intF//range of ValuesTen intI, J, K;//Control Loop Variables One intSum, Max;//and the maximum value A intM, N;//record sub-array -clock_t start, end;//record the start and end times -cout <<"Please enter a range of values:"; theCIN >>F; -start = Clock ();//assign the current system's time call to start -Srand ((unsigned) time (NULL));/*Initializes a random number for the seed - There's no such thing as code + The resulting random number should be the same plus this code - The random number generated is random.*/ + for(i =0; i<n; i++)//control Loops, A { atA[i] = (rand ()%2? -1:1) *rand ()% F;//Generate random numbers -cout << A[i] <<" ";//output one-dimensional arrays - } -cout <<Endl; -max = a[0];//The first number in the array as the maximum value first - //The following is a comparison of the maximum value in for(i =0; i<n; i++) - { to for(j =0; J <= I; J + +) + { -sum =0;//initialize the and of the word group the for(k = j; k <= i; k++)//Loop Variable *sum = sum +A[k]; $ if(sum>max)Panax Notoginseng { -max = sum;//Update Max: The and plus next number from the first subarray becomes the and of the new sub-array, comparing the maximum value them =J; +n = i;//can't record the i,j in the loop. A } the } + } -end = Clock ();//End of Time $cout <<"run time is:"<<Double((End-start))/ +<<"seconds"<< Endl;//Run Time $cout <<"the maximum number of sub-arrays and is:"<< Max <<Endl; -cout <<"the sub-array is the first"<< m +1<<"number, to the first"<< n +1<<"number. "<<Endl; -cout <<"the sub-arrays are:"; the for(i = m; i <= N; i++) - {Wuyicout << A[i] <<" "; the } -cout <<Endl; Wu}
Experiment:
Array Size: 5000
Running time: 57.202s
Design ideas: After the macro defines the array of operands, the input array of the range of values, starting from the definition of the first sub-array to compare and find the maximum value of the sub-array, and record the start and end of time.
Implementation time Flow: Wednesday (April 8)
|
|
|
| events |
time period |
time |
| design ideas |
< td> |
1h30m" |
| Online find example |
4:30-5:00 |
30m |
| explanatory statement |
6:00-8:00 |
2h |
| 8:00-8:30 |
|
Summary: What will not, I should learn to ask others, check information, can not always keep the computer in a daze. And try to remember what you've seen.
Maximum sub-array-productivity