結果

問題 No.708 (+ー)の式
ユーザー bombrary_skbombrary_sk
提出日時 2019-03-29 20:02:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,675 bytes
コンパイル時間 529 ms
コンパイル使用メモリ 81,520 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 16:43:17
合計ジャッジ時間 1,285 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;

#define REP(i,n) for(ll (i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(),v.end()
#define Decimal fixed << setprecision(20)
#define SHOWP(x) cerr<<"["<<(x).FI<<", "<<(x).SE<<"]";
#define SHOWX(x) cerr<<#x<<": "<<x<<endl;
#define SHOWVEC(v, e) REP(i, e) cerr << (v[i]) << ' '; cerr << endl;
#define SHOW2D(a, h, w) REP(i, h){REP(j, w)cerr<<setw(3)<<(a[i][j])<<' ';cerr<<endl;}
constexpr int INF = 1 << 30 - 1;
constexpr long long LLINF = 1LL << 60;
constexpr long long MOD = 1000000007;

typedef long long ll;
typedef pair<ll, ll> P;

using State = string::const_iterator;

int expr(State &s);
int term(State &s);
int factor(State &s);
int number(State &s);

int number(State &s)
{
  int ret = 0;
  while (isdigit(*s)) {
    ret *= 10;
    ret += *s - '0';
    s++;
  }
  return ret;
}

int factor(State &s)
{
  int ret;
  if (*s == '(') {
    s++;
    ret = expr(s);
    s++;
  } else {
    ret = number(s);
  }
  return ret;
}

int term(State &s)
{
  int ret = factor(s);
  while (1) {
    if (*s == '(') ret *= factor(s);
    else break;
  }
  return ret;
}

int expr(State &s)
{
  int ret = term(s);
  while (1) {
    if (*s == '+') {
      s++;
      ret += term(s);
    } else if (*s == '-') {
      s++;
      ret -= term(s);
    } else {
      break;
    }
  }
  return ret;
}

int main()
{
  string S;
  cin >> S;

  State begin = S.begin();
  cout << expr(begin) << endl;

  return 0;
}
0