코드
import java.util.Scanner;
public class Main {
static int n;
static int[] numbers;
static int[] operators = new int[4];
static StringBuilder sb = new StringBuilder();
static int max = Integer.MIN_VALUE;
static int min = Integer.MAX_VALUE;
public static void main(String[] args) {
input();
func(0, numbers[0]);
sb.append(max).append('\n').append(min);
System.out.println(sb);
}
static void input() {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
numbers = new int[n];
for (int i = 0; i < n; ++i) {
numbers[i] = scanner.nextInt();
}
for (int i = 0; i < 4; ++i) {
operators[i] = scanner.nextInt();
}
scanner.close();
}
static void func(int rec, int value) {
if (rec == n - 1) {
min = Integer.min(min, value);
max = Integer.max(max, value);
return;
}
for (int i = 0; i < 4; ++i) {
if (operators[i] > 0) {
--operators[i];
func(rec + 1, calculate(value, i, numbers[rec + 1]));
++operators[i];
}
}
}
static int calculate(int operand1, int operator, int operand2) {
switch (operator) {
case 0 :
return operand1 + operand2;
case 1 :
return operand1 - operand2;
case 2 :
return operand1 * operand2;
default :
return operand1 / operand2;
}
}
}