有志コン 逆ポーランド記法
K RPN
問題概要
リンク参照
考察
- stack使って実装します.
- 乗除算が入るとそれはそれで実装しないとねぇ・・・
ソースコード
#include<iostream> #include<vector> #include<algorithm> #include<cctype> #include<utility> #include<string> #include<cmath> #include<cstring> #include<queue> #include<map> #include<set> #include<climits> #include<bitset> #include<stack> using namespace std; /******************************************************************************************/ int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; stack<int> st; for (int i = 0; i < n; i++) { string s; cin >> s; if (s == "+" || s == "-") { int ans; if (s == "-") { ans = -st.top(); st.pop(); ans += st.top(); st.pop(); } else { ans = st.top(); st.pop(); ans += st.top(); st.pop(); } st.push(ans); } else { st.push(stoi(s)); } } cout << st.top() << endl; }