結果

問題 No.708 (+ー)の式
ユーザー 👑 CleyL
提出日時 2022-02-18 00:44:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 757 ms
コンパイル使用メモリ 73,680 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-29 07:55:06
合計ジャッジ時間 1,402 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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