結果

問題 No.49 算数の宿題
ユーザー risujirohrisujiroh
提出日時 2018-11-05 02:28:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 915 bytes
コンパイル時間 1,504 ms
コンパイル使用メモリ 165,296 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 17:40:37
合計ジャッジ時間 2,058 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T> void assign(V<T>& v, int n, const T& a = T()) { v.assign(n, a); }
template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); }


using Itr = string::const_iterator;
int expr(Itr& itr);
int num(Itr& itr);

int expr(Itr& itr) {
  int res = num(itr);
  while (true) {
    if (*itr == '*') res += num(++itr);
    else if (*itr == '+') res *= num(++itr);
    else break;
  }
  return res;
}

int num(Itr& itr) {
  int res = 0;
  while (isdigit(*itr)) {
    res = res * 10 + *itr - '0';
    ++itr;
  }
  return res;
}

int main() {
  cin.tie(nullptr); ios_base::sync_with_stdio(false);
  string s; cin >> s;
  Itr itr = cbegin(s);
  cout << expr(itr) << '\n';
}
0