[ BOJ Code ]/# BOJ -

[ 백준 15658 ] 연산자 끼워넣기(2) (C++)

얍문 2019. 2. 8. 22:03

백준의 연산자끼워넣기(2)(15658) 문제이다.

[ 문제 바로가기 ]


[ 문제풀이 ]

1) 이 문제를 풀기 전, 연산자끼워넣기(1) 을 풀어보고 오는것을 추천한다. 굉장히 비슷한 문제이기 때문이다.

   [ 연산자끼워넣기(1) 문제 바로가기 ]

   [ 연산자끼워넣기(1) 문제풀이 바로가기 ]


   이 문제도 연산자끼워넣기(1) 과 풀이가 굉장히 비슷하다. 사실 다른점이 없다....

   위의 풀이를 보고 충분히 이해했다면 쉽게 풀 수 있을 문제이다.


[ 소스코드 ]

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
61
62
63
#include<iostream>
 
#define endl "\n"
#define MAX 11
using namespace std;
 
int Max_Value = -1000000001;
int Min_Value = 1000000001;
int N;
int Arr[MAX];
int Plus, Minus, Multiple, Divide;
 
void Input()
{
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        cin >> Arr[i];
    }
    
    cin >> Plus >> Minus >> Multiple >> Divide;
}
 
void DFS(int P, int Mi, int Mul, int D, int Sum, int N_Idx)
{
    if (N_Idx == N)
    {
        if (Sum > Max_Value) Max_Value = Sum;
        if (Sum < Min_Value) Min_Value = Sum;
        return;
    }
 
    if (P < Plus) DFS(P + 1, Mi, Mul, D, Sum + Arr[N_Idx], N_Idx + 1);
    if (Mi < Minus) DFS(P, Mi + 1, Mul, D, Sum - Arr[N_Idx], N_Idx + 1);
    if (Mul < Multiple) DFS(P, Mi, Mul + 1, D, Sum * Arr[N_Idx], N_Idx + 1);
    if (D < Divide) DFS(P, Mi, Mul, D + 1, Sum / Arr[N_Idx], N_Idx + 1);
}
 
void Solution()
{
    DFS(0000, Arr[0], 1);
 
    cout << Max_Value << endl;
    cout << Min_Value << endl;
}
 
void Solve()
{
    Input();
    Solution();
}
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    //freopen("Input.txt", "r", stdin);
    Solve();
 
    return 0;
}
cs