일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 다익스트라
- back propagation
- pytorch
- lazy propagation
- 2023
- 플로이드 와샬
- 회고록
- dfs
- 알고리즘
- 이분 탐색
- 미래는_현재와_과거로
- 문자열
- 백트래킹
- 세그먼트 트리
- dropout
- 너비 우선 탐색
- 크루스칼
- 분할 정복
- BFS
- 조합론
- DP
- tensorflow
- 자바스크립트
- NEXT
- Overfitting
- 가끔은 말로
- 가끔은_말로
- object detection
- c++
- 우선 순위 큐
Archives
- Today
- Total
Doby's Lab
[알고리즘] 백준 15658번: 연산자 끼워넣기 (2) (C++) 본문
https://www.acmicpc.net/problem/15658
15658번: 연산자 끼워넣기 (2)
N개의 수로 이루어진 수열 A1, A2, ..., AN이 주어진다. 또, 수와 수 사이에 끼워넣을 수 있는 연산자가 주어진다. 연산자는 덧셈(+), 뺄셈(-), 곱셈(×), 나눗셈(÷)으로만 이루어져 있다. 연산자의 개수
www.acmicpc.net
연산자 배열을 선언 후 각 연산자를 고른 경우 총 4가지를 가지고서 백트래킹 하면 된다.
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <climits>
using namespace std;
int n;
vector<int> num;
int oper[4] = { 0, };
int maxValue = INT_MIN;
int minValue = INT_MAX;
void backTrack(int cnt, int sum) {
if (cnt == n) {
maxValue = max(maxValue, sum);
minValue = min(minValue, sum);
return;
}
for (int i = 0; i < 4; i++) {
if (i == 0 && oper[i] > 0) {
oper[i] -= 1;
backTrack(cnt + 1, sum + num[cnt]);
oper[i] += 1;
}
else if (i == 1 && oper[i] > 0) {
oper[i] -= 1;
backTrack(cnt + 1, sum - num[cnt]);
oper[i] += 1;
}
else if (i == 2 && oper[i] > 0) {
oper[i] -= 1;
backTrack(cnt + 1, sum * num[cnt]);
oper[i] += 1;
}
else if (i == 3 && oper[i] > 0) {
oper[i] -= 1;
backTrack(cnt + 1, sum / num[cnt]);
oper[i] += 1;
}
}
}
int main() {
cin >> n;
int value;
for (int i = 0; i < n; i++) {
cin >> value;
num.push_back(value);
}
for (int i = 0; i < 4; i++) {
cin >> oper[i];
}
backTrack(1, num[0]);
cout << maxValue << '\n';
cout << minValue;
return 0;
}
'PS > BOJ' 카테고리의 다른 글
[알고리즘] 백준 17404번: RGB거리 2 (C++), 의도적 코드 (0) | 2021.11.29 |
---|---|
[알고리즘] 백준 9252번: LCS 2 (C++) (0) | 2021.11.29 |
[알고리즘] 백준 10819번: 차이를 최대로 (C++) (0) | 2021.11.28 |
[알고리즘] 백준 10769번: 행복한지 슬픈지 (C++) (0) | 2021.11.27 |
[알고리즘] 백준 15489번: 파스칼 삼각형 (C++) (0) | 2021.11.27 |