[백준] 14888번 연산자 끼워넣기 C++

semi·2022년 8월 23일
0

coding test

목록 보기
36/57

https://www.acmicpc.net/problem/14888

#include <iostream>
#include <vector>
#include <string>

using namespace std;

vector<int> v;
vector<int> oper;
vector<vector<int>> oper_comb;
vector<int> v_tmp;
int N;
bool checked[11][11] = { 0, };
void DFS(int cnt)
{
	if (cnt == N - 1)
	{
		oper_comb.push_back(v_tmp);
	}
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < oper[i]; j++)
		{
			if (!checked[i][j])
			{
				checked[i][j] = true;
				v_tmp.push_back(i);
				DFS(cnt + 1);
				checked[i][j] = false;
				v_tmp.pop_back();
			}

		}
	}
}

int main(void)
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	cin >> N;
	int tmp, max_val = -1000000001, min_val = 1000000001;
	for (int i = 0; i < N; i++)
	{
		cin >> tmp;
		v.push_back(tmp);
	}
	for (int i = 0; i < 4; i++)
	{
		cin >> tmp;
		oper.push_back(tmp);
	}
	DFS(0);
	for (int i = 0; i < oper_comb.size(); i++)
	{
		int tmp_sum = v[0];
		for (int j = 0; j < oper_comb[i].size(); j++)
		{
			if (oper_comb[i][j] == 0)
			{
				tmp_sum += v[j + 1];
			}
			else if (oper_comb[i][j] == 1)
			{
				tmp_sum -= v[j + 1];
			}
			else if (oper_comb[i][j] == 2)
			{
				tmp_sum *= v[j + 1];
			}
			else if (oper_comb[i][j] == 3)
			{
				tmp_sum /= v[j + 1];
			}
		}
		max_val = max(max_val, tmp_sum);
		min_val = min(min_val, tmp_sum);
	}
	cout << max_val << endl;
	cout << min_val;
	return 0;
}

0개의 댓글