justgo_developer

[JAVA] 백준 14888 연산자 끼워넣기 본문

IT/코딩 문제 풀이

[JAVA] 백준 14888 연산자 끼워넣기

다날92 2018. 12. 1. 23:44
728x90
반응형
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
import java.util.*;
public class Main {
    
    static int N;
    static int[] number;
    static int[] op = new int[4];
    static int min = Integer.MAX_VALUE;
    static int max = Integer.MIN_VALUE;
    
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        N = sc.nextInt();
        number = new int[N+1];
        for(int i=0;i<N;i++){
            number[i] = sc.nextInt();
        }
        for(int i=0;i<4;i++){
            op[i] = sc.nextInt();
        }
        dfs(0,number[0],0);
        System.out.println(max);
        System.out.println(min);
        
    }
    public static void dfs(int num, int result,int j){
        if(num == N-1){
            if(max<result)
                max = result;
            if(min>result)
                min = result;
        }
        
        for(int i=0;i<4;i++){
            if(op[i]>0){
                if(i==0){
                    op[i]--;
                    dfs(num+1,result + number[j+1],j+1);
                    op[i]++;
                }
                else if(i==1){
                    op[i]--;
                    dfs(num+1,result - number[j+1],j+1);
                    op[i]++;
                }
                else if(i==2){
                    op[i]--;
                    dfs(num+1,result * number[j+1],j+1);
                    op[i]++;
                }
                else if(i==3){
                    op[i]--;
                    dfs(num+1,result / number[j+1],j+1);
                    op[i]++;
                }
            }
        }
    }
    
    
}
cs


728x90
반응형

'IT > 코딩 문제 풀이' 카테고리의 다른 글

[JAVA] 백준 14890 경사로  (0) 2018.12.01
[JAVA] 백준 14889 스타트와 링크  (0) 2018.12.01
[JAVA] 백준 15655 N과 M (6)  (0) 2018.09.10
[JAVA] 백준 15657 N과 M (8)  (0) 2018.09.10
[JAVA] 백준 1012 유기농배추  (0) 2018.01.10