結果

問題 No.49 算数の宿題
ユーザー re_re0101re_re0101
提出日時 2021-10-27 05:50:46
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,049 bytes
コンパイル時間 1,987 ms
コンパイル使用メモリ 199,240 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-24 17:50:45
合計ジャッジ時間 2,604 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,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=number(s,p);
        while(p<(int)s.size()){
            if(s[p]=='*'){
                p++;
                res+=number(s,p);
                continue;
            }
            if(s[p]=='+'){
                p++;
                res*=number(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