Member: Liu Wei Http://www.cnblogs.com/Lw-1573/p/5323542.html
1. Title: Returns the and of the largest sub-array in an integer array.
Requirements: Enter an array of shapes, with positive numbers in the array and negative values. 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). 2. Design idea: The most difficult problem of this topic is how to find the sub-array and the time complexity is O (n); My idea is to start traversing the entire array from the left (A[0]) to the rightmost end (a[n-1]), and record the largest subarray and Maxsofar in the process so far. The Maxsofar is initialized to 0. What if we have found the largest sub-array between a[0] and A[n-1] and the largest subarray between a[0] and A[i]? The largest sub-array between "or a[0" to a[i-1] and ", or" from A[i, "the maximum number of consecutive numbers to go forward." In order to start from a[i], the maximum value of several consecutive numbers is used as follows: Starting from a[i] The maximum value of several consecutive numbers is maxending_i equal to (maxending_i-1) +a[i] and the maximum of 2, i.e. Maxending_i=max ((manending_i-1) +a[i],0) 3 code:
1 #include <iostream> 2 using namespace std; 3 int max (int a,int b) 4 {5 if (a>b) 6 {7 return A; 8 } 9 else10 {One return b;12 }13} int maxsum (int a[], int n) { 0 int i;17 int Maxsofar The maximum value of the Maxsofar record so far is maxendinghere = 0;//maxendinghere records the maximum value of the number of consecutive digits starting at the current position (i = 0; i < n; i++) { maxendinghere = max (Maxendinghere + a[i], 0); maxsofar = max (Maxsofar, maxendinghere); 23< C16/>}24 return maxsofar;25}26 int main () { int n, i=0;29 cout<< "Please enter Number:"; >n;31 cout<< "Please enter an array:"; int a[100000]={0};33 for (i=0;i<n;i++) >>a[i];36 }37 int Max=maxsum (A, n); cout << Maximum sub-array: << Max << endl;39 return 0;40}
4. Summary
At first it was intended to find each sub-array, and then to find the maximum value, when the program code is found to be troublesome, and a lot of code function is repeated, so I began to read the data structure of the last semester textbooks, to find the appropriate ideas for this topic. When I finish writing, I feel that if I start to define the length of the array, then the scope of the application is limited, so I think how to make the length of the array can be arbitrarily changed, and then I think of a freshman learning C + + When the pointer can define a dynamic array, so in the program used the pointer
Project Plan Summary:
| Date/Task |
Lectures |
Writing Programs |
Read related books |
Total Day |
| Monday |
110 |
60 |
120 |
290 |
| Tuesday |
|
|
120 |
120 |
| Wednesday |
|
60 |
120 |
180 |
| Thursday |
110 |
60 |
90 |
320 |
| Friday |
|
120 |
60 |
180 |
| Saturday |
|
|
60 |
60 |
| Sunday |
|
|
60 (PLAN) |
60 |
| Weekly totals |
220 |
360 |
330 |
910 |
Time log:
| Date |
Start time |
End time |
Interrupt Time |
NET time |
Activities |
Note |
| 3/7 |
14:00 |
15:50 |
14:50 |
100 |
Class |
|
|
19:00 |
22:00 |
|
180 |
Reading |
Human Moon Myth |
| 3/8 |
19:00 |
22:00 |
20:30 |
180 |
Programming |
Homework |
| 3/9 |
19:00 |
22:00 |
|
180 |
Programming |
Homework |
| 3/10 |
14:00 |
15:50 |
14:50 |
100 |
Class |
Software Unit Testing |
|
19:00 |
22:00 |
|
180 |
Reading |
The method of construction |
| 3/11 |
14:30 |
18:30 |
|
240 |
Study |
Homework |
| 3/12 |
8:00 |
11:30 |
|
210 |
Programming |
Rest (Plan) |
| 3/13 |
8:00 |
11:30 |
|
180 |
Review |
Rest (Plan) |
Bug log:
| 3/8 |
1 |
Design |
Compile |
10min |
Dynamic planning |
Returns the maximum number of sub-arrays in an integer array and