본문 바로가기

code/BOJ

백준 14888 연산자 끼워넣기

문제:

 

14888번: 연산자 끼워넣기

첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수, 곱셈(×)의 개수, 나눗셈(÷)의 개수이다. 

www.acmicpc.net

코드:

변수명이 엉망이다...

#include <iostream>
#include <algorithm>
#define f(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
int N;
int oprn[11];
int oprt[10], o;
int ub = -1000000000;
int lb = 1000000000;
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N;
	f(n, N) cin >> oprn[n];
	f(o1, 4) {
		int os; cin >> os;
		f(o2, os)
			oprt[o++] = o1;
	}
	do {
		int s = oprn[0];
		f(e, o) 
			switch (oprt[e]) {
			case 0: s += oprn[e + 1]; break;
			case 1: s -= oprn[e + 1]; break;
			case 2: s *= oprn[e + 1]; break;
			case 3: s /= oprn[e + 1]; break;
			}
		if (s < lb) lb = s;
		if (s > ub) ub = s;
	} while (next_permutation(oprt, oprt + o));
	cout << ub << '\n' << lb;
	return 0;
}