forked from lolosssss/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path343_integer_break.c
More file actions
57 lines (49 loc) · 938 Bytes
/
343_integer_break.c
File metadata and controls
57 lines (49 loc) · 938 Bytes
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
/**
* Description : Integer Break
* Given a positive integer n, break it into the sum of at least
* two positive integers and maximize the product of those
* integers. Return the maximum product you can get.
* Author : Evan Lau
* Date : 2016/04/20
*/
#include <stdio.h>
int integerBreak(int n)
{
int i = 0;
int ret = 1;
if (n == 2)
{
return 1;
}
if (n == 3)
{
return 2;
}
switch (n % 3)
{
case 1:
i = n / 3 - 1;
ret *= 4;
break;
case 2:
i = n / 3;
ret *= 2;
break;
default:
i = n / 3;
break;
}
while (i--)
{
ret *= 3;
}
return ret;
}
int main(void)
{
for (int i = 2; i < 21; i++)
{
printf("i = %d : %d\n", i, integerBreak(i));
}
return 0;
}