-
[백준] 14888 - 연산자 끼워넣기Algorithm 문제 2019. 7. 12. 16:21
https://www.acmicpc.net/problem/14888
14888번: 연산자 끼워넣기
첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, 곱셈(×)의 개수, 나눗셈(÷)의 개수이다.
www.acmicpc.net
간단한 브루트 포스. 재귀 함수로 구현
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#include <cstdio>#include <algorithm>#include <limits.h>using namespace std;int op[4], num[12];int n, re_min = INT_MAX, re_max = INT_MIN;void recursion(int depth, int c){if(depth == n){re_min = min(c, re_min);re_max = max(c, re_max);return;}for(int i=0; i<4; i++){if(op[i] > 0){op[i]--;int t;switch(i){case 0:t = c + num[depth];break;case 1:t = c - num[depth];break;case 2:t = c * num[depth];break;case 3:t = c / num[depth];break;}recursion(depth+1, t);op[i]++;}}return;}int main(){scanf("%d", &n);for(int i=0; i<n; i++) scanf("%d", &num[i]);for(int i=0; i<4; i++) scanf("%d", &op[i]);recursion(1, num[0]);printf("%d\n%d", re_max, re_min);return 0;}cs 'Algorithm 문제' 카테고리의 다른 글
[백준] 14500 - 테트로미노 (0) 2019.07.15 [백준] 13460 - 구슬 탈출 2 (0) 2019.07.13 [백준] 17144 - 미세먼지 안녕! (0) 2019.07.08 [백준] 1064 - 평행사변형 (0) 2019.07.03 [백준] 15954 - 인형들 (1) 2019.07.02