본문 바로가기
IT/알고리즘

백준 14888 연산자 끼워넣기

by 어센트 2020. 2. 24.

https://www.acmicpc.net/problem/14888

 

14888번: 연산자 끼워넣기

첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, 곱셈(×)의 개수, 나눗셈(÷)의 개수이다. 

www.acmicpc.net

백트래킹 문제였는데 처음에 매개변수를 하나로만 받고 결과를 전역변수로 하여 문제를 해결하려 했는데 정확히 나오지 않아서 매개변수를 2개로 바꿔서 계속 함수호출할 때 마다 전달하는 방식으로 문제를 해결했다.

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
package 백트래킹;
import java.io.*;
 
public class 연산자_끼워넣기 {
    static int n;
    static int[] op = new int[4];
    static int[] num;
    static int  max = Integer.MIN_VALUE,min = Integer.MAX_VALUE;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        num = new int[n];
        String[] in = br.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            num[i] = Integer.parseInt(in[i]);
        }
        in = br.readLine().split(" ");
        for (int i = 0; i < 4; i++) {
            op[i] = Integer.parseInt(in[i]);
        }
        fun(1,num[0]);
        System.out.println(max);
        System.out.println(min);
        br.close();
    }
    public static void fun(int depth,int ans){
        if(depth == n){
            max = Math.max(ans,max);
            min = Math.min(ans,min);
            return;
        }
        for (int i = 0; i < 4; i++) {
            if(op[i]>0){
                op[i]--;
                if(i==0){
                    fun(depth+1,ans+num[depth]);
                }
                else if(i==1){
                    fun(depth+1,ans-num[depth]);
                }
                else if(i==2){
                    fun(depth+1,ans*num[depth]);
                }
                else {
                    fun(depth+1,ans/num[depth]);
                }
                op[i]++;
            }
        }
    }
 
 
}
 
cs

 

다른 분들이 푼 것을 참고하여 다시 푼 것

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
package 백트래킹;
 
import java.io.*;
 
public class 연산자_끼워넣기다시 {
    static int[] nums;
    static int[] op = new int[4];
    static int n, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
    static int sum = 0;
 
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        n = Integer.parseInt(br.readLine());
        nums = new int[n];
        String[] in = br.readLine().split(" ");
        for (int i = 0; i < n; i++) {
            nums[i] = Integer.parseInt(in[i]);
        }
        in = br.readLine().split(" ");
        for (int i = 0; i < 4; i++) {
            op[i] = Integer.parseInt(in[i]);
        }
        fun(1, nums[0]);
        System.out.println(max);
        System.out.println(min);
    }
 
    public static void fun(int depth, int sum) {
        if (depth > n - 1) {
            max = Math.max(sum, max);
            min = Math.min(sum, min);
            return;
        }
        if (op[0> 0) {
            op[0]--;
            fun(depth + 1, sum + nums[depth]);
            op[0]++;
        }
        if (op[1> 0) {
            op[1]--;
            fun(depth + 1, sum - nums[depth]);
            op[1]++;;
        }
        if (op[2> 0) {
            op[2]--;
            fun(depth + 1, sum * nums[depth]);
            op[2]++;;
        }
        if (op[3> 0) {
            op[3]--;
            fun(depth + 1, sum / nums[depth]);
            op[3]++;;
        }
 
    }
}
 
cs

'IT > 알고리즘' 카테고리의 다른 글

백준 1929 소수구하기  (0) 2020.02.29
백준 14889 스타트와 링크  (0) 2020.02.26
백준 9663 N-Queen  (0) 2020.02.22
백준 15650 N과 M(2)  (0) 2020.02.20
백준 15649 N 과M(1)  (0) 2020.02.20