-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
54 lines (47 loc) · 1.5 KB
/
Main.java
File metadata and controls
54 lines (47 loc) · 1.5 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
import java.util.Scanner;
public class Main {
static int minVal=Integer.MAX_VALUE, maxVal=Integer.MIN_VALUE, N=0;
static int[] operater, number;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
number = new int[N];
for(int i=0; i<N; i++){
number[i] = sc.nextInt();
}
operater = new int[4]; // +, -, x, / 의 개수
for(int i=0; i<operater.length; i++){
operater[i] = sc.nextInt();
}
dfs(1, number[0]);
System.out.println(maxVal);
System.out.println(minVal);
sc.close();
}
public static void dfs(int depth, int sum){
if(depth == N){
minVal = Math.min(minVal, sum);
maxVal = Math.max(maxVal, sum);
}
for(int i=0; i<4; i++){
if(operater[i] > 0){
operater[i]--;
switch(i){
case 0: // +
dfs(depth+1, sum + number[depth]);
break;
case 1: // -
dfs(depth+1, sum - number[depth]);
break;
case 2: // x
dfs(depth+1, sum * number[depth]);
break;
case 3: // /
dfs(depth+1, sum / number[depth]);
break;
}
operater[i]++;
}
}
}
}