-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sugar.java
90 lines (79 loc) · 1.78 KB
/
Sugar.java
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
package mathmatics;
import java.util.Scanner;
public class Sugar {
private static void dynamic(){
final int MAXVALUE = 5000;
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int d[][] = new int[3][N + 1];
int packages[] = {0, 3, 5};
for (int i = 0; i < 3; i++) {
for (int j = 0; j <= N; j++) {
d[i][j] = MAXVALUE;
}
}
d[0][0] = 0;
for (int i = 1; i < 3; i++) {
for (int j = 0; j <= N; j++) {
if(packages[i] > j)
d[i][j] = d[i - 1][j];
else if(d[i - 1][j] > d[i][j - packages[i]] + 1)
d[i][j] = d[i][j - packages[i]] + 1;
else
d[i][j] = d[i - 1][j];
}
}
if(d[2][N] == MAXVALUE)
System.out.println(-1);
else
System.out.println(d[2][N]);
}
/*
* x = 5의 갯수
* y = 3의 갯수
* 나머지 x y
* 0 (N/5) 0
* 1 (N/5)-1 2
* 2 (N/5)-2 4
* 3 (N/5) 1
* 4 (N/5)-1 3
*
* cf) (N/5)-1과 (N/5)-2가 음수가 되는 경우 (4와 7)은 별도로 고려해주어야함.
* */
private static void math(){
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int x[] = {N /5, (N / 5) - 1, (N / 5) - 2, (N / 5), (N / 5) - 1};
int y[] = {0, 2, 4, 1, 3};
if(N == 4 || N == 7)
System.out.println(-1);
else
System.out.println(x[N % 5] + y[N % 5]);
}
private static void math2(){
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
if(N == 4 || N == 7)
System.out.println(-1);
else {
switch(N % 5){
case 0:
System.out.println(N/5);
break;
case 1:
case 3:
System.out.println(N/5 + 1);
break;
case 2:
case 4:
System.out.println(N/5 + 2);
break;
}
}
}
public static void main(String[] args) {
// dynamic();
// math();
math2();
}
}