Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Effective Java
- MySQL
- Chunk
- REACT
- current_date
- nodejs
- upgrade
- Spring Batch
- log_bin
- Effective Java 3/e
- mysql 5.5
- spring
- 정규표현식
- migration
- 퀵소트
- Express
- Regular expression
- regex
- eslint
- Node
- update
- expire_logs_days
- REACTJS
- git
- log4j2
- java
- spring cloud
- Webpack
- npm
- JavaScript
Archives
- Today
- Total
내 세상
[Algorithm] 백준 14888번 연산자 끼워넣기 본문
728x90
백준 14888번 연산자 끼워넣기
https://www.acmicpc.net/problem/14888
말 그대로 연산자를 끼워넣으면 된다.
특정 위치(pos)에서 남아 있는 연산자를 순차적으로 넣어본 후 최대값과 최소값을 찾으면 됨.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> #include <stdio.h> #include <algorithm> #include <vector> #define ll long long int using namespace std; int N; ll val[12]; int expr[4]; int exprCnt; ll maxVal = -1e10; ll minVal = 1e10; void dfs(int pos, int cnt, ll result) { if (pos == N) { if (result > maxVal) maxVal = result; if (result < minVal) minVal = result; return; } for (int i = 0; i < 4; i++) { if (expr[i] > 0) { int resultOrigin = result; switch (i) { case 0: result += val[pos]; break; case 1: result -= val[pos]; break; case 2: result *= val[pos]; break; case 3: result /= val[pos]; break; } expr[i] --; dfs(pos + 1, cnt - 1, result); expr[i] ++; result = resultOrigin; } } } int main() { cin >> N; for (int i = 0; i < N; i++) { cin >> val[i]; } exprCnt = 0; for (int i = 0; i < 4; i++) { cin >> expr[i]; exprCnt += expr[i]; } dfs(1, exprCnt, val[0]); cout << maxVal << endl << minVal; return 0; } | cs |
728x90
'Coding > Algorithms' 카테고리의 다른 글
[Algorithm] 백준 15683번 감시 (0) | 2019.03.17 |
---|---|
[Algorithm] 백준 14889번 스타트와 링크 (0) | 2019.03.17 |
[Algorithm] 백준 13458번 시험 감독 (0) | 2019.03.17 |
[Algorithm] 백준 14501번 퇴사 (1) | 2019.03.17 |
[Algorithm] 백준 15686번 치킨 배달 (0) | 2019.03.17 |