結果
問題 | No.708 (+ー)の式 |
ユーザー |
|
提出日時 | 2018-06-29 23:15:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,210 bytes |
コンパイル時間 | 1,599 ms |
コンパイル使用メモリ | 171,524 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 00:13:49 |
合計ジャッジ時間 | 2,146 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; stack<int> op, optmp; bool braket = false; int ans = -1e5; int tmp = -1e5; for (int i = 0; i < (int) s.size(); i++) { if (s[i] == '+') { if (!braket) op.push(1); else optmp.push(1); } else if (s[i] == '-') { if (!braket) op.push(-1); else optmp.push(-1); } else if (s[i] == '(') { braket = true; } else if (s[i] == ')') { braket = false; if (ans == -1e5) { ans = tmp; } else { ans += op.top() * tmp; op.pop(); } tmp = -1e5; } else { int n = s[i] - '0'; if (braket) { if (tmp == -1e5) { tmp = n; if (!optmp.empty()) { tmp *= optmp.top(); optmp.pop(); } } else { tmp += optmp.top() * n; optmp.pop(); } } else { if (ans == -1e5) { ans = n; if (!op.empty()) { ans *= op.top(); op.pop(); } } else { ans += op.top() * n; op.pop(); } } } } cout << ans << endl; return 0; }