-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion14.cpp
More file actions
53 lines (49 loc) · 1.17 KB
/
Copy pathquestion14.cpp
File metadata and controls
53 lines (49 loc) · 1.17 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
/*
剪绳子
给你一根长度为n的绳子,请把绳子剪成m段(m、n都是整数,n>1并且m>1),每段绳子的长度记为k[0],k[1],...,k[m]。
请问k[0]*k[1]*...*k[m]可能的最大乘积是多少?
例如,当绳子的长度是8时,我们把它剪成长度为2、3、3的三段,此时得到的乘积是18。
Xiaobin Tian;
*/
#include <vector>
#include<math.h>
using namespace::std;
int maxCutting_1(int n){
if(n == 1)
return 0;
if(n == 2)
return 1;
if(n == 3)
return 2;
int* result = new int[n+1];
result[0] = 0;
result[1] = 1;
result[2] = 2;
result[3] = 3;
int max = 0;
for(int i = 4; i <= n; ++i){
max = 0;
for(int j = 1; j <= i/2; ++j){
int temp = result[j] * result[i-j];
if(temp > max)
max = temp;
}
result[i] = max;
}
max = result[n];
return max;
}
int maxCutting_2(int n){
if(n == 1)
return 0;
if(n == 2)
return 1;
if(n == 3)
return 2;
int num3 = n / 3;
if(n % 3 == 1)
num3 -= 1;
int max = pow(3, num3) + pow(2, 2);
return max;
}