ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

[๋ฌธ์ œ]

[ํ’€์ด]

N๊ฐœ์˜ ์ˆ˜์˜ ์ˆœ์„œ๋Š” ๊ณ ์ •์ด์–ด์„œ, ์‚ฌ์ด์‚ฌ์ด์— ์—ฐ์‚ฐ์ž๋ฅผ ๋„ฃ์–ด ์กฐํ•ฉํ•ด ๊ณ„์‚ฐํ•ด์ฃผ๋ฉด ๋˜๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

 

์šฐ์„  number[]์— ์ˆซ์ž๋ฅผ ์ €์žฅํ•˜๊ณ , operator[]์— ๊ฐ ์—ฐ์‚ฐ๋“ค์˜ ํ‚ค๊ฐ’์˜ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•œ makeSet()๋ฅผ ํ†ตํ•ด 4์น™ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉฐ min๊ณผ max๊ฐ’์„ ๋ฝ‘์•„๋‚ด์–ด ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

 

[์ฝ”๋“œ]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
    // ์—ฐ์‚ฐ์ž ๋ผ์›Œ๋„ฃ๊ธฐ
    public static int N;
    public static int[] number;
    public static int[] operator; // ์—ฐ์‚ฐ์ž๋“ค์˜ ํ‚ค๊ฐ’์„ ๋„ฃ์–ด์คŒ
    public static int max = Integer.MIN_VALUE;
    public static int min = Integer.MAX_VALUE;
    public static ArrayList<String> setArr = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        number = new int[N];
        StringTokenizer st = new StringTokenizer(br.readLine());
        for(int i=0;i<N;i++) {
            number[i] = Integer.parseInt(st.nextToken());
        }
        operator = new int[4];
        st = new StringTokenizer(br.readLine());
        for(int i=0;i<4;i++) {
            operator[i] = Integer.parseInt(st.nextToken());
        }

        makeSet(number[0],1);
        System.out.println(max);
        System.out.println(min);
    }
    public static void makeSet(int num, int index) {
        if(index == N) {
            max = Math.max(max, num);
            min = Math.min(min, num);
            return;
        }
        for(int i=0;i<4;i++) {
            if(operator[i] > 0) {
                operator[i]--;
                switch (i){
                    case 0:
                        makeSet(num + number[index],index + 1);
                        break;
                    case 1:
                        makeSet(num - number[index],index + 1);
                        break;
                    case 2:
                        makeSet(num * number[index],index + 1);
                        break;
                    case 3:
                        makeSet(num / number[index],index + 1);
                        break;
                }
                operator[i]++;
            }
        }
    }
}