14888 연산자 끼워넣기
2019. 8. 7. 13:39ㆍ알고리즘/백준
순열을 통한 완전탐색 문제
문제: https://www.acmicpc.net/problem/14888
깃허브주소: https://github.com/surinoel/boj/blob/master/14888.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <climits> | |
#include <iostream> | |
#include <algorithm> | |
using namespace std; | |
int main(void) { | |
ios_base::sync_with_stdio(false); | |
cin.tie(nullptr); | |
int n; | |
cin >> n; | |
vector<int> a(n); | |
for (int i = 0; i < n; i++) { | |
cin >> a[i]; | |
} | |
vector<char> op; | |
for (int i = 0; i < 4; i++) { | |
int cnt; | |
cin >> cnt; | |
for (int j = 0; j < cnt; j++) { | |
if (i == 0) op.push_back('+'); | |
else if (i == 1) op.push_back('-'); | |
else if (i == 2) op.push_back('*'); | |
else if (i == 3) op.push_back('/'); | |
} | |
} | |
int minval = INT_MAX, maxval = INT_MIN; | |
sort(op.begin(), op.end()); | |
do { | |
int start = a[0]; | |
for (int i = 0; i < n - 1; i++) { | |
if (op[i] == '+') start += a[i + 1]; | |
else if (op[i] == '-') start -= a[i + 1]; | |
else if (op[i] == '*') start *= a[i + 1]; | |
else if (op[i] == '/') start /= a[i + 1]; | |
} | |
minval = min(minval, start); | |
maxval = max(maxval, start); | |
} while (next_permutation(op.begin(), op.end())); | |
cout << maxval << '\n' << minval << '\n'; | |
return 0; | |
} |
'알고리즘 > 백준' 카테고리의 다른 글
17140 이차원 배열과 연산 (0) | 2019.08.09 |
---|---|
15685 드래곤 커브 (0) | 2019.08.09 |
1952 수영장 (0) | 2019.08.06 |
1188 음식 평론가 (0) | 2019.08.05 |
3023 마술사 이민혁 (0) | 2019.08.04 |