https://www.acmicpc.net/problem/15658
15658번: 연산자 끼워넣기 (2)
N개의 수로 이루어진 수열 A1, A2, ..., AN이 주어진다. 또, 수와 수 사이에 끼워넣을 수 있는 연산자가 주어진다. 연산자는 덧셈(+), 뺄셈(-), 곱셈(×), 나눗셈(÷)으로만 이루어져 있다. 연산자의 개수
www.acmicpc.net
pair<int, int> calc(vector<int>& num, int index, int cur, int plus, int minus, int mul, int div)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
pair<int, int> calc(vector<int>& num, int index, int cur, int plus, int minus, int mul, int div)
{
int n = num.size();
if (index == n)
{
return make_pair(cur, cur);
}
vector<pair<int, int>> res;
if(plus > 0)
res.push_back(calc(num, index + 1, cur + num[index], plus - 1, minus, mul, div));
if(minus > 0)
res.push_back(calc(num, index + 1, cur - num[index], plus, minus - 1, mul, div));
if(mul > 0)
res.push_back(calc(num, index + 1, cur * num[index], plus, minus, mul - 1, div));
if (div > 0)
res.push_back(calc(num, index + 1, cur / num[index], plus, minus, mul, div - 1));
auto ans = res[0];
for (auto p : res)
{
//최대값
if (ans.first < p.first)
ans.first = p.first;
//최소값
if (ans.second > p.second)
ans.second = p.second;
}
//pair를 return
return ans;
}
int main()
{
int n;
cin >> n;
int plus, minus, mul, div;
vector<int> num(n);
for (int i = 0; i < n; i++)
cin >> num[i];
cin >> plus >> minus >> mul >> div;
auto p = calc(num, 1, num[0], plus, minus, mul, div);
cout << p.first << '\n';
cout << p.second << '\n';
return 0;
}
|
cs |
'브루트 포스 > 브루트포스(재귀)' 카테고리의 다른 글
테트로미노 (중요!) (0) | 2023.04.01 |
---|---|
선발 명단 (0) | 2023.03.31 |
부분수열의 합 (재귀, 비트마스크) (0) | 2023.03.21 |
로또 (0) | 2023.03.20 |
맞춰봐 (백트래킹) (0) | 2023.03.01 |