結果

問題 No.708 (+ー)の式
ユーザー けーむけーむ
提出日時 2020-03-13 12:06:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,823 bytes
コンパイル時間 1,484 ms
コンパイル使用メモリ 168,696 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-14 02:34:29
合計ジャッジ時間 2,417 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0