結果

問題 No.708 (+ー)の式
ユーザー CleyLCleyL
提出日時 2022-02-18 00:44:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 73,228 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-11 17:57:49
合計ジャッジ時間 1,380 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <stack>
using namespace std;
int main(){
    string s;cin>>s;
    s = "0+"+s;
    stack<int> x;
    for(int i = 0; s.size() > i; i++){
      //cout << s[i] << s[i+1] << endl;
        if('0' <= s[i] && s[i] <= '9'){
            x.push(s[i]-'0');
        }else if(i+1 != s.size() && s[i+1] == '('){
            stack<int> y;
            char l = s[i];
            i+=2;
            while(s[i] != ')'){
                if('0' <= s[i] && s[i] <= '9'){
                    y.push(s[i]-'0');
                    i++;
                }else if(s[i] == '+'){
                    int z = y.top();y.pop();
                    y.push(z+s[i+1]-'0');
                    i+=2;
                }else{
                    int z = y.top();y.pop();
                    y.push(z-(s[i+1]-'0'));
                    i+=2;
                }
            }
            int b = x.top();x.pop();
            if(l == '+'){
                x.push(b+y.top());
            }else{
                x.push(b-y.top());
            }
            
        }else{
            int b = x.top();x.pop();
            if(s[i] == '+'){
                x.push(b+s[i+1]-'0');
                i++;
            }else{
                x.push(b-(s[i+1]-'0'));
                i++;
            }
        }
        //cout << x.top() << endl;
    }
    cout << x.top() << endl;
}
0