Recent Posts
Recent Comments
Link
- Today
- Yesterday
- Total
메이쁘
(JAVA) 백준 14888번 : 연산자 끼워넣기 본문
https://www.acmicpc.net/problem/14888
14888번: 연산자 끼워넣기
첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, ��
www.acmicpc.net
브루트 포스 문제.
순열 을 사용했다.
순열을 통해 부호를 순차적으로 선택하며, 선택할 때 마다 차곡차곡 계산해서 패러미터로 담아 재귀호출한다.
그래서 모든 부호를 선택한 경우에 지금까지 계산한 결과값들 중 최솟값을 찾아 출력하면 끝!
더 자세한 사항은 아래 소스코드를 참고해주세요.
감사합니다.
소스코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.util.LinkedList; | |
import java.util.StringTokenizer; | |
// 연산자 끼워넣기 | |
// 브루트 포스 | |
public class p14888 { | |
static int max = Integer.MIN_VALUE; | |
static int min = Integer.MAX_VALUE; | |
static int[] numArr; | |
public static void main(String args[]) throws IOException { | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
int n = Integer.parseInt(br.readLine()); | |
numArr = new int[n]; | |
int[] buhoArr = new int[4]; | |
// 숫자들 입력 받기 | |
StringTokenizer st = new StringTokenizer(br.readLine()); | |
for(int i = 0; i < n; i++) { | |
int num = Integer.parseInt(st.nextToken()); | |
numArr[i] = num; | |
} | |
// 문자들 입력받기 | |
st = new StringTokenizer(br.readLine()); | |
for(int i = 0; i < 4; i++) { // 0: +, 1: -, 2: *, 3: / | |
int buhoCount = Integer.parseInt(st.nextToken()); | |
buhoArr[i] = buhoCount; | |
} | |
permutation(n, 1, buhoArr, numArr[0]); | |
System.out.println(max); | |
System.out.println(min); | |
} | |
// 순열(부호 순서있게 뽑기) | |
static void permutation(int n, int index, int[] buhoArr, int now) { | |
if(index == n) { | |
max = max > now ? max : now; | |
min = min < now ? min : now; | |
return; | |
} | |
else { | |
for(int i = 0; i < buhoArr.length; i++) { | |
if(buhoArr[i] > 0) { | |
buhoArr[i]--; | |
permutation(n, index + 1, buhoArr, calculate(now, numArr[index], i)); | |
buhoArr[i]++; | |
} | |
} | |
} | |
} | |
// 계산하는 함수 | |
static void calculation(LinkedList<Integer> resultArr) { | |
int i = 1; | |
int result = numArr[0]; | |
for(int buho : resultArr) { | |
result = calculate(result, numArr[i++], buho); | |
} | |
max = max > result ? max : result; | |
min = min < result ? min : result; | |
} | |
// 부호에 맞게 연산해서 결과값을 리턴하는 함수 | |
static int calculate(int a, int b, int buho) { | |
switch(buho) { | |
case 0: // 덧셈 | |
return a + b; | |
case 1: // 뺄셈 | |
return a - b; | |
case 2: // 곱셈 | |
return a * b; | |
case 3: // 나눗셈 | |
return a / b; | |
} | |
return 0; | |
} | |
} |
'Algorithm > Baekjoon' 카테고리의 다른 글
(JAVA) 백준 2792번 : 보석 상자 --- [이진탐색] (1) | 2020.06.05 |
---|---|
(JAVA) 백준 6603번 : 로또 --- [백트래킹] (0) | 2020.06.04 |
(JAVA) 백준 1620번 : 나는야 포켓몬 마스터 이다솜 (3) | 2020.06.04 |
(JAVA) 백준 1038번 : 감소하는 수 (0) | 2020.06.03 |
(JAVA) 백준 6588번 : 골드바흐의 추측 (0) | 2020.06.01 |