結果
| 問題 |
No.708 (+ー)の式
|
| コンテスト | |
| ユーザー |
けーむ
|
| 提出日時 | 2020-03-13 12:06:24 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,823 bytes |
| コンパイル時間 | 1,834 ms |
| コンパイル使用メモリ | 170,848 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-21 20:34:44 |
| 合計ジャッジ時間 | 2,211 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
ll f(string s) {
ll tmp = 0, ans = 0;
bool plus = false, minus = false;
s += "=";
rep(i, len(s)) {
if ((s[i] >= '0') && (s[i] <= '9')) {
tmp = tmp * 10 + (s[i] - '0');
continue;
}
if ((!plus) && (!minus)) ans = tmp;
else if (plus) ans += tmp;
else ans -= tmp;
plus = false; minus = false;
if (s[i] == '+') plus = true;
else if (s[i] == '-') minus = true;
tmp = 0;
}
return ans;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// ifstream in("input.txt");
// cin.rdbuf(in.rdbuf());
string s;
cin >> s;
string ns = "", tmp = "";
bool fb = false;
rep(i, len(s)) {
if (s[i] == '(') {
fb = true;
tmp = "";
}
else if (s[i] == ')') {
ll v = f(tmp);
ns += to_string(v);
fb = false;
}
else {
if (fb) tmp += s[i];
else ns += s[i];
}
}
string nns = "";
nns += ns[0];
reps(i, 1, len(ns)) {
if ((ns[i - 1] == '+') && (ns[i] == '-')) {
nns[len(nns) - 1] = '-';
continue;
}
if ((ns[i - 1] == '-') && (ns[i] == '-')) {
nns[len(nns) - 1] = '+';
continue;
}
nns += ns[i];
}
cout << f(nns) << endl;
return 0;
}
けーむ