- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14888.java
More file actions
Latest commit
78 lines (58 loc) · 1.96 KB
/
Copy path14888.java
File metadata and controls
78 lines (58 loc) · 1.96 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
importjava.util.*;
publicclassMain {
publicstaticvoidmain(String[] args) {
Scannersc = newScanner(System.in);
intn = sc.nextInt();
int[] nums = newint[n];
for(inti=0; i<n; i++) {
nums[i] = sc.nextInt();
}
intplus = sc.nextInt();
intminus = sc.nextInt();
intmultiple = sc.nextInt();
intdivide = sc.nextInt();
calc(nums, newint[n-1], 0, plus, minus, multiple, divide);
System.out.println(max);
System.out.println(min);
}
publicstaticintmax = -1000000001;
publicstaticintmin = 1000000001;
publicstaticvoidcalc(int[] nums, int[] operators, inti, intplus, intminus, intmultiple, intdivide) {
// operators가 다 찼으면 계산해서 결과 저장
if(i == operators.length) {
intresult = nums[0];
for(intj=0; j<operators.length; j++) {
if(operators[j] == 1)
result += nums[j+1];
elseif(operators[j] == 2)
result -= nums[j+1];
elseif(operators[j] == 3)
result *= nums[j+1];
elseif(operators[j] == 4)
result /= nums[j+1];
}
if(result > max)
max = result;
if(result < min)
min = result;
return;
}
if(plus > 0) {
operators[i] = 1;
calc(nums, operators, i+1, plus-1, minus, multiple, divide);
}
if(minus > 0) {
operators[i] = 2;
calc(nums, operators, i+1, plus, minus-1, multiple, divide);
}
if(multiple > 0) {
operators[i] = 3;
calc(nums, operators, i+1, plus, minus, multiple-1, divide);
}
if(divide > 0) {
operators[i] = 4;
calc(nums, operators, i+1, plus, minus, multiple, divide-1);
}
return;
}
}