-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboj2839.cpp
86 lines (72 loc) · 1.11 KB
/
boj2839.cpp
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
// Dynamic Programming - silver4
#include<iostream>
using namespace std;
int func(int N);
int fun(int N);
int ary[5001] = { };
int main()
{
int N;
cin >> N;
cout << "sol : " << fun(N) << endl;
//for(int i=0;i<=N;i++)
// cout << ary[i] << " ";
return 0;
}
int func(int N)
{
if (ary[N] != 0) return ary[N];
cout << N << endl;
int tmp = 9999;
if (N < 3)
{
ary[N] = -1;
return ary[N];
}
else if (1) // 3, 5 multi check
{
if (N % 5 == 0)
return N / 5;
if (N % 3 == 0)
{
tmp = (N / 3) < tmp ? (N / 3) : tmp;
//return N / 3;
}
}
if (N > 3)
ary[N] = func(N - 3) + 1 < tmp ? func(N - 3) + 1 : tmp;
if (tmp == 9999)
{
ary[N] = -1;
tmp = -1;
}
return ary[N];
}
int fun(int N)
{
if (ary[N] != 0) return ary[N];
//if (N == 3) return 1;
int tmp;
if (N < 3)
{
ary[N] = -1;
return ary[N];
}
else if (N % 5 == 0)
return ary[N] = N / 5;
else if (N % 3 == 0)
{
tmp = N / 3;
if (N > 3)
ary[N] = fun(N - 3) + 1 < tmp ? fun(N - 3) + 1 : tmp;
else ary[N] = tmp;
}
else
{
int bf = fun(N - 3);
if (bf > 0) ary[N] = bf + 1;
else
return -1;
}
return ary[N];
}