結果

問題 No.708 (+ー)の式
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2019-02-24 02:38:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,181 bytes
コンパイル時間 1,275 ms
コンパイル使用メモリ 146,832 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-22 07:14:07
合計ジャッジ時間 2,178 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
int gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
#define MOD 1000000007

int cal(string s) {
    int a;
    size_t len=s.length();
    if (s[len-1]!=')') {
        a=(int)s[len-1]-'0';
        if (s.length()>1) {
            switch(s[len-2]) {
                case '+':
                    a=cal(s.substr(0,len-2))+a;
                    break;
                case '-':
                    a=cal(s.substr(0,len-2))-a;
                    break;
            }
        }
    }
    else {
        size_t i=len;
        while(s[i]!='(') {
            i--;
        }
        a=cal(s.substr(i+1,len-i-2));
        if (0<i-1) {
            switch(s[i-1]) {
                case '+':
                    a=cal(s.substr(0,i-1))+a;
                    break;
                case '-':
                    a=cal(s.substr(0,i-1))-a;
                    break;
            }
        }
    }
    return a;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    cout << cal(s) << endl;
    return 0;
}
0