BOJ 14888 연산자 끼워넣기

1 개요[ | ]

BOJ 14888 연산자 끼워넣기


2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int N;
int operands[11];
int operators[4];
int MIN = 1000000000;
int MAX = -1000000000;

void solve(int in, int idx) {
    if(idx == N) {
        if(in > MAX) MAX = in;
        if(in < MIN) MIN = in;
        return;
    }
    for(int i=0; i<4; i++) {
        if(operators[i] > 0) {
            operators[i]--;
            if(i == 0) {
                solve(in+operands[idx], idx+1);
            } else if(i == 1) {
                solve(in-operands[idx], idx+1);
            } else if(i == 2) {
                solve(in*operands[idx], idx+1);
            } else {
                solve(in/operands[idx], idx+1);
            }
            operators[i]++;
        }
    }
}

int main() {
    cin >> N;
    for(int i=0; i<N; i++) {
        cin >> operands[i];
    }
    for(int i=0; i<4; i++) {
        cin >> operators[i];
    }
    solve(operands[0], 1);
    cout << MAX << '\n';
    cout << MIN;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}