結果
問題 | No.708 (+ー)の式 |
ユーザー |
|
提出日時 | 2018-07-06 15:06:26 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,201 bytes |
コンパイル時間 | 556 ms |
コンパイル使用メモリ | 68,420 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-01 02:46:04 |
合計ジャッジ時間 | 1,181 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include <cstdlib> #include <iostream> #include <sstream> #include <stack> #include <string> static inline int eval(std::istringstream& ss) noexcept { int x = 0; char op = '\0'; for (auto ch = ss.get(); ch != EOF; ch = ss.get()) { switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if (op == '\0') { x = ch - '0'; } else { auto y = ch - '0'; if (op == '+') { x += y; } else { x -= y; } } break; case '+': case '-': op = static_cast<char>(ch); break; case '(': if (op == '\0') { x = eval(ss); } else { if (op == '+') { x += eval(ss); } else { x -= eval(ss); } } break; case ')': return x; } } return x; } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); std::string s; std::cin >> s; std::istringstream ss(s); std::cout << eval(ss) << std::endl; return EXIT_SUCCESS; }