공부기록/백준
[백준] 14888 연산자 끼워넣기
메델
2023. 12. 18. 00:43
import java.util.Scanner;
public class Main {
static int N;
static int[] nums, operators;
static int max, min;
static StringBuilder sb = new StringBuilder();
public static int calculator(int operand1,int operator, int operand2) {
if(operator == 1) {
return operand1 + operand2;
}else if(operator == 2) {
return operand1 - operand2;
}else if(operator == 3) {
return operand1 * operand2;
}else {
return operand1 / operand2;
}
}
public static void rec_func(int k, int value) {
if(k == N) {
max = Math.max(max, value);
min = Math.min(min, value);
}else {
for(int i=1; i<5; i++) {
if(operators[i]>=1) {
operators[i]--;
rec_func(k+1, calculator(value, i, nums[k+1]) );
operators[i]++;
}
}
}
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
N = kb.nextInt();
nums = new int[N+1];
operators = new int[5];
for(int i=1; i<N+1; i++) {
nums[i] = kb.nextInt();
}
for(int i=1; i<5; i++) {
operators[i] = kb.nextInt();
}
max = Integer.MIN_VALUE;
min = Integer.MAX_VALUE;
rec_func(1, nums[1]);
sb.append(max).append('\n').append(min);
System.out.println(sb);
}
}