-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15658.java
78 lines (58 loc) · 1.96 KB
/
15658.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
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for(int i=0; i<n; i++) {
nums[i] = sc.nextInt();
}
int plus = sc.nextInt();
int minus = sc.nextInt();
int multiple = sc.nextInt();
int divide = sc.nextInt();
calc(nums, new int[n-1], 0, plus, minus, multiple, divide);
System.out.println(max);
System.out.println(min);
}
public static int max = -1000000001;
public static int min = 1000000001;
public static void calc(int[] nums, int[] operators, int i, int plus, int minus, int multiple, int divide) {
// operators가 다 찼으면 계산해서 결과 저장
if(i == operators.length) {
int result = nums[0];
for(int j=0; j<operators.length; j++) {
if(operators[j] == 1)
result += nums[j+1];
else if(operators[j] == 2)
result -= nums[j+1];
else if(operators[j] == 3)
result *= nums[j+1];
else if(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;
}
}