점점 강해지고 있습니다.
[Java] 백준 14888번: 연산자 끼워넣기
풀이 방법
주어진 숫자들 사이에 연산자를 임의로 끼워넣어 계산된 결과가 최대, 최소를 구하는 문제이다.
여기서 연산자의 우선순위에 따라 계산하지 않고 순서대로 계산한다는 조건이 있었다.
입력된 수 사이사이에 연산자만 순열로 가능한 모든 순서를 맞춰 끼워넣고 결과값을 도출한 후, 최대값과 최소값을 갱신하면 되는 간단한 문제였다.
풀이 코드
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ_14888_연산자_끼워넣기 {
static char[] CHS = {'+','-','*','/'};
static char[] OPERATOR;
static int[] OPERAND;
static int MIN, MAX;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
OPERATOR = new char[N-1];
OPERAND = new int[N];
MIN = Integer.MAX_VALUE;
MAX = Integer.MIN_VALUE;
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++) {
OPERAND[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
int opNum = 0;
for(int i=0; i<4; i++) {
int operators = Integer.parseInt(st.nextToken());
for(int j=0; j<operators; j++) {
OPERATOR[opNum++] = CHS[i];
}
}
perm(new char[OPERATOR.length], new boolean[OPERATOR.length], 0);
System.out.println(MAX+"\n"+MIN);
}
private static void perm(char[] selected, boolean[] visited, int depth) {
if(depth == selected.length) {
int tmp = OPERAND[0];
for(int i=1; i<OPERAND.length; i++) {
if(selected[i-1] == '+') tmp += OPERAND[i];
else if(selected[i-1] == '-') tmp -= OPERAND[i];
else if(selected[i-1] == '*') tmp *= OPERAND[i];
else if(selected[i-1] == '/') tmp /= OPERAND[i];
}
MIN = Math.min(MIN, tmp);
MAX = Math.max(MAX, tmp);
return;
}
for(int i=0; i<selected.length; i++) {
if(!visited[i]) {
visited[i] = true;
selected[depth] = OPERATOR[i];
perm(selected, visited, depth+1);
visited[i] = false;
}
}
}
}
Thank You For Reading