[BOJ / C++] 14888 연산자 끼워넣기

Seulguo·2022년 7월 20일
0

Algorithm

목록 보기
108/185
post-thumbnail
post-custom-banner

🐣 문제

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


🐥 코드

#include <iostream>
using namespace std;

int N;
int operands[11]; 
int operators[4]; 
int mymin = 1000000001;
int mymax = -1000000001;

void backtracking(int result, int idx){
    if(idx == N){
        if(result > mymax)
            mymax = result;
        if(result < mymin)
            mymin = result;
        return;
    }

    for(int i = 0; i < 4; i++){
        if(operators[i] > 0){
            operators[i]--; 
            if(i == 0)
                backtracking(result + operands[idx], idx+1);
            else if(i == 1)
                backtracking(result - operands[idx], idx+1);
            else if(i == 2)
                backtracking(result * operands[idx], idx+1);
            else
                backtracking(result / operands[idx], idx+1);
            operators[i]++; 
        }
    }
    return;
}

int main() {
    cin >> N;
    for(int i = 0; i < N; i++)
        cin >> operands[i];
    for(int i = 0; i < 4; i++)
        cin >> operators[i];

    backtracking(operands[0],1);

    cout << mymax << '\n';
    cout << mymin;
}
post-custom-banner

0개의 댓글