結果

問題 No.708 (+ー)の式
ユーザー re_re0101re_re0101
提出日時 2021-10-27 05:45:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,043 bytes
コンパイル時間 2,313 ms
コンパイル使用メモリ 197,444 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 07:34:31
合計ジャッジ時間 3,027 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl>vvl;
typedef vector<vvl>vvvl;
typedef vector<vvvl>vvvvl;
typedef vector<vvvvl>vvvvvl;
typedef vector<int>vi;
typedef vector<vi>vvi;
typedef vector<vvi>vvvi;
typedef vector<vvvi>vvvvi;
typedef vector<vvvvi>vvvvvi;
typedef pair<ll,ll> P;
typedef string::const_iterator State;

ll exp(ll n,ll r){if(r==0)return 1;return n*exp(n,r-1);}

template<typename T>
struct Parser{
    bool error;
    Parser():error(false){}

    T expression(string s,int &p){
        T res=term(s,p);
        while(p<(int)s.size()){
            if(s[p]=='+'){
                p++;
                res+=term(s,p);
                continue;
            }
            if(s[p]=='-'){
                p++;
                res-=term(s,p);
                continue;
            }
            break;
        }
        return res;
    }
 
    T term(string s,int &p){
        T res=factor(s,p);
        while(p<(int)s.size()){
            if(s[p]=='*'){
                p++;
                res*=factor(s,p);
                continue;
            }
            if(s[p]=='/'){
                p++;
                T d=factor(s,p);
                if(d==T(0)){
                    error=true;
                    break;
                }
                res/=d;
                continue;
            }
            break;
        }
        return res;
    }
 
    T factor(string &s,int &p){
        T res;
        if(s[p]=='('){
            p++;
            res=expression(s,p);
            p++;
        }else{
            res=number(s,p);
        }
        return res;
    }
 
    T number(string s,int &p){
        T res=0;
        while(p<(int)s.size() and isdigit(s[p])) res=res*10+s[p++]-'0';
        return res;
    }
 
 
    T execute(string s){
        int p=0;
        error=false;
        return expression(s,p);
    }
};

int main(){
    
    Parser<int>ps;
    string s;cin>>s;
    cout<<ps.execute(s)<<endl;
}
0