Skip to content

Commit

Permalink
14888 연산자 끼워넣기 / 브루트포스, 백트래킹
Browse files Browse the repository at this point in the history
  • Loading branch information
cjy8922 authored Apr 4, 2021
1 parent 7aa303e commit 907b2bd
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 2021_03/BJ_14888.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;

int nummax = -1000000001;
int nummin = 1000000001;
int number[100];
int sachic[4];

void solution(int res, int idx, int size) {
if (idx == size) {
if (res >= nummax) nummax = res;
if (res <= nummin) nummin = res;
return;
}

else {
for (int i = 0; i < 4; i++) {
if (sachic[i] != 0) {
sachic[i]--;
if (i == 0) solution(res + number[idx], idx + 1, size);
else if (i == 1) solution(res - number[idx], idx + 1, size);
else if (i == 2) solution(res * number[idx], idx + 1, size);
else if (i == 3) solution(res / number[idx], idx + 1, size);
sachic[i]++;
}
}
}
}


int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n, plus, minus, multi, div;
cin >> n;
for (int i = 0; i < n; i++) cin >> number[i];
cin >> sachic[0] >> sachic[1] >> sachic[2] >> sachic[3];

solution(number[0], 1, n);
cout << nummax << endl << nummin << endl;
return 0;
}

0 comments on commit 907b2bd

Please sign in to comment.