문제
입력 및 출력
풀이
import java.io.*;
import java.util.*;
class Main {
public static int N;
public static int[] array;
public static int[] operator;
public static int MAX = Integer.MIN_VALUE;
public static int MIN = Integer.MAX_VALUE;
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
array = new int[N];
String[] temp = br.readLine().split(" ");
for (int i = 0; i < N; i++) {
array[i] = Integer.parseInt(temp[i]);
}
operator = new int[4];
String[] temp2 = br.readLine().split(" ");
for (int i = 0; i < 4; i++) {
operator[i] = Integer.parseInt(temp2[i]);
}
DFS(array[0], 1);
System.out.println(MAX);
System.out.println(MIN);
}
public static void DFS(int number, int index) {
if (index == N) {
MAX = Math.max(MAX, number);
MIN = Math.min(MIN, number);
}
for (int i = 0; i < 4; i++) {
if (operator[i] > 0) {
operator[i]--;
switch (i) {
case 0:
DFS(number + array[index], index + 1);
break;
case 1:
DFS(number - array[index], index + 1);
break;
case 2:
DFS(number * array[index], index + 1);
break;
case 3:
DFS(number / array[index], index + 1);
break;
}
operator[i]++;
}
}
}
}
결과 및 해결방법
[결과]