본문 바로가기
Algorithm

BruteForce_백준 연산자 끼워넣기

by forkballpitch 2021. 11. 10.
728x90
728x90

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

import java.io.*;
import java.util.StringTokenizer;

/*
int 범위 -21억 ~ 21
 */
public class Main {

    static int N,max,min;
    static int[] nums, operators, order;
    static StringBuilder sb = new StringBuilder();

    static void input(){
        FastReader scan = new FastReader();
        N = scan.nextInt();
        nums = new int[N+1];
        operators = new int[5];
        order = new int[N+1];
        for(int i = 1; i <= N; i++)
            nums[i] = scan.nextInt();
        for(int i = 1; i <=4; i++)
            operators[i] = scan.nextInt();

        max = Integer.MIN_VALUE;
        min = Integer.MAX_VALUE;

    }

    static int calculator(){
        int value = nums[1];
        for(int i = 1; i <= N-1; ++i){
            if(order[i] == 1)
                value = value + nums[i+1];
            if(order[i] == 2)
                value = value - nums[i+1];
            if(order[i] == 3)
                value = value * nums[i+1];
            if(order[i] == 4)
                value = value / nums[i+1];
        }
        return value;
    }


    public static void solution(int k){
        if(k == N){ //모든 연산자들을 전부 나열하는 방법을 찾았다.
            //정한 연산자 순서대로 계산해서 정답을 갱신
            int value = calculator();
            max = Math.max(max, value);
            min = Math.min(min, value);
        }else{ //k번째 연산자는 무엇을 선택할 것인가
            //4가지 연산자들 중에 뭘 쓸 것인지 선택하고 재귀호출하기
            for(int cand = 1; cand <=4; cand++){
                if(operators[cand] >=1){
                    operators[cand]--;
                    order[k] = cand;
                    solution(k+1);
                    operators[cand]++;
                    order[k] = 0;
                }
            }
        }

    }
    public static void main(String[] args) {
        input();
        solution(1);
        sb.append(max).append('\n').append(min);
        System.out.println(sb.toString());

    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(new File(s)));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
728x90
728x90