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.
Design ideas:
Using the idea of dynamic programming, this problem is solved by translating the question into a larger one between a subarray containing the current array element and a sub-array that does not contain the current array element, and forwards forward, finally becoming the larger of the first element and 0.
Source:
1 //calculates the sum of the largest sub-arrays in a column array, Li Qing, hu Jinhui2#include <iostream>3 using namespacestd;4 #defineMax (x, y) (x>y?x:y)5 intMain ()6 {7 intI,length=0, list[ +];//define loop variables, array lengths, arrays8 intSUM1;//records the sum of the largest sub-arrays containing the current array elements9 intsum2;//records the sum of the largest sub-arrays that do not contain the current array elementsTencout <<"Please enter an array element:"<<Endl; One while(Cin >> list[length++]) && getchar ()! ='\ n'); Acout <<"you typed the"<< length <<"number"<<Endl; - -SUM1 = list[0]; thesum2 =0; - for(i =1; i < length; i++) - { -sum2 =Max (sum2, sum1); +SUM1 = max (sum1 +List[i], list[i]); - } +SUM1 =Max (sum2, sum1); Acout <<"The value of the largest sub-array in the array you entered is:"<<sum1<<Endl; at return 0; -}
Results:
Summarize:
The experiment is mainly applied to the idea of dynamic planning, at first it is not too clear what the idea is, and then in the teacher's explanation to understand, and finally wrote the program, in general this time there is a great harvest.
Project Plan Summary:
Date/Task |
Lectures |
Programming |
Read related books |
Find information online |
Total Day |
Monday |
100 |
20 |
10 |
10 |
140 |
Tuesday |
|
30 |
|
10 |
40 |
Wednesday |
|
100 |
30 |
20 |
150 |
Thursday |
100 |
30 |
|
|
130 |
Friday |
|
30 |
|
20 |
50 |
Saturday |
|
30 |
30 |
|
60 |
Sunday |
|
|
|
|
|
Week Summary |
200 |
240 |
70 |
60 |
570 |
Time Recording Log
Date |
Start time |
End time |
Interrupt Time |
NET time |
Activities |
Note |
3/21 |
14:00 |
15:50 |
10 |
100 |
Lectures |
Software Engineering Classes |
|
16:00 |
16:20 |
0 |
20 |
Programming |
|
|
16:30 |
16:40 |
0 |
10 |
Read related books |
The law of Construction |
|
17:00 |
17:10 |
0 |
10 |
Find information online |
|
3/22 |
19:00 |
19:30 |
0 |
30 |
Programming |
|
|
19:40 |
19:50 |
0 |
10 |
Find information online |
|
3/23 |
14:00 |
16:00 |
20 |
100 |
Programming |
|
|
16:30 |
17:00 |
0 |
30 |
Read related books |
"Dream Break Code" |
|
17:10 |
17:30 |
0 |
20 |
Find information online |
|
3/24 |
14:00 |
15:50 |
10 |
100 |
Lectures |
Software Engineering Classes |
|
19:00 |
19:30 |
0 |
30 |
Programming |
|
3/25 |
19:00 |
19:30 |
0 |
30 |
Programming |
|
|
20:00 |
20:20 |
0 |
20 |
Find information online |
|
3/26 |
9:00 |
9:30 |
0 |
30 |
Programming |
|
|
10:00 |
10:30 |
0 |
30 |
Read related books |
The law of Construction |
Bug log:
Date |
Number |
Type |
Introduction phase |
Exclusion phase |
Repair time |
Note |
3/22 |
1 |
20 |
Coding |
Compile |
1min |
Variable not defined sum2 |
3/25 |
2 |
20 |
Coding |
Compile |
1min |
Loop Nesting output error |
Pair development partner: Hu Jinhui http://www.cnblogs.com/hujinhui/
Maximum sub-array one