Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 다이나믹 프로그래밍
- 브루트포스
- DFS
- 백준
- 유니온 파인드
- 알고리즘
- 정렬
- 시뮬레이션
- 그리디 알고리즘
- c++
- 구현
- VR
- 수학
- 누적 합
- 투 포인터
- 다익스트라
- 자료구조
- ue5
- XR Interaction Toolkit
- 우선순위 큐
- 유니티
- Unreal Engine 5
- 재귀
- 백트래킹
- Team Fortress 2
- 문자열
- 트리
- BFS
- 스택
- 그래프
Archives
- Today
- Total
1일1알
백준 1935번 후위 표기식2 C++ 본문
https://www.acmicpc.net/problem/1935
스택 사용, float가 아닌 double 사용해야 함.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
string str;
cin >> n >> str;
map<char, double> m;
for (int i = 0; i < n; i++) {
int val;
cin >> val;
m['A' + i] = val;
}
stack <double> st;
for (int i = 0; i < str.length(); i++) {
if (str[i] == '*' || str[i] == '+' || str[i] == '/' || str[i] == '-') continue;
str[i] = m[str[i]];
}
for (int i = 0; i < str.length(); i++) {
if (str[i] == '*' || str[i] == '+' || str[i] == '/' || str[i] == '-') {
double val1 = st.top();
st.pop();
double val2 = st.top();
st.pop();
double val;
switch (str[i]) {
case '*':
val = val2 * val1;
break;
case '+':
val = val2 + val1;
break;
case '/':
val = val2 / val1;
break;
case '-':
val = val2 - val1;
break;
}
st.push(val);
}
else {
double val = (int)str[i];
st.push(val);
}
}
cout << fixed;
cout.precision(2);
cout << st.top();
};
'알고리즘' 카테고리의 다른 글
백준 21318번 피아노 체조 C++ (0) | 2023.03.14 |
---|---|
백준 3758번 KCPC C++ (0) | 2023.03.12 |
백준 16168번 퍼레이드 C++ (0) | 2023.03.08 |
백준 16398번 행성 연결 C++ (0) | 2023.03.07 |
백준 1411번 비슷한 단어 C++ (0) | 2023.03.05 |