-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicPlan.cpp
More file actions
90 lines (68 loc) · 1.97 KB
/
Copy pathDynamicPlan.cpp
File metadata and controls
90 lines (68 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <time.h>
#include "charUtils.h"
using namespace std;
int cacheProductAfterCutRope(int n) {
if (n <= 0)
{
cout << "Params invalid";
return 0;
} else if (n <= 4) {
return n;
}
int* maxResults = new int[n + 1];
maxResults[0] = 0;
maxResults[1] = 1;
maxResults[2] = 2;
maxResults[3] = 3;
int max = 0;
for (int i = 4; i<= n; ++i) {
max = 0;
for (int j = 1; j <= i/2; j++)
{
int maxResult = maxResults[j] * maxResults[i - j];
if (max < maxResult)
{
max = maxResult;
}
maxResults[i] = max;
}
}
max = maxResults[n];
delete[] maxResults;
return max;
}
int maxProductAfterCutRope(int n) {
if (n <= 0)
{
cout << "Params invalid";
return 0;
} else if (n <= 4) {
return n;
}
int i = 1;
int maxValue = 5;
while (i <= n / 2 ) {
int result = maxProductAfterCutRope(i) * maxProductAfterCutRope(n - i);
if (result > maxValue)
{
maxValue = result;
cout << "---------- max value change ----------";
}
cout << "index: " << i << '\n' << ", this result value: " << result;
i++;
}
return maxValue;
}
// test command: clang++ DynamicPlan.cpp charUtils.cpp -o roap && ./roap 10
int main(int argc, char* args[]) {
int n = stringToNumber(args[1]);
time_t startTime, endTime;
time(&startTime);
// int maxValue = maxProductAfterCutRope(n);
int maxValue = cacheProductAfterCutRope(n);
time(&endTime);
cout << "cost time: " << (difftime(endTime, startTime)) << '\n';
cout << "max result: " << maxValue << '\n';
return 0;
}