4008 숫자 만들기

2019. 8. 8. 14:10알고리즘/삼성

백준 14888 연산자 끼워넣기와 비슷한 문제. 순열을 통해서 차이의 최대를 구하는 문제

 

문제: https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIeRZV6kBUDFAVH

깃허브주소: https://github.com/surinoel/boj/blob/master/swea4008.cpp

 

#include <vector>
#include <climits>
#include <iostream>
#include <algorithm>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tc;
cin >> tc;
for (int test_case = 1; test_case <= tc; test_case++) {
int n;
cin >> n;
vector<int> num(n);
vector<char> opvector;
for (int i = 0; i < 4; i++) {
int x; cin >> x;
for (int j = 0; j < x; j++) {
if (i == 0) opvector.push_back('+');
else if (i == 1) opvector.push_back('-');
else if (i == 2) opvector.push_back('*');
else if (i == 3) opvector.push_back('/');
}
}
for (int i = 0; i < n; i++) {
cin >> num[i];
}
sort(opvector.begin(), opvector.end());
int minval, maxval;
minval = INT_MAX, maxval = INT_MIN;
do {
int start = num[0];
for (int i = 0; i < opvector.size(); i++) {
if (opvector[i] == '+') start += num[i + 1];
else if (opvector[i] == '-') start -= num[i + 1];
else if (opvector[i] == '*') start *= num[i + 1];
else if (opvector[i] == '/') start /= num[i + 1];
}
minval = min(minval, start);
maxval = max(maxval, start);
} while (next_permutation(opvector.begin(), opvector.end()));
cout << "#" << test_case << " " << maxval - minval << '\n';
}
return 0;
}
view raw swea4008.cpp hosted with ❤ by GitHub

'알고리즘 > 삼성' 카테고리의 다른 글

2477 차량 정비소  (0) 2019.08.09
5656 벽돌 깨기  (0) 2019.08.08
5644 무선 충전  (0) 2019.08.08
2117 홈 방범 서비스  (0) 2019.08.08
4014 활주로 건설  (0) 2019.08.07