結果
| 問題 |
No.708 (+ー)の式
|
| ユーザー |
bombrary_sk
|
| 提出日時 | 2019-03-29 20:02:52 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,675 bytes |
| コンパイル時間 | 2,029 ms |
| コンパイル使用メモリ | 82,716 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-11-07 01:23:15 |
| 合計ジャッジ時間 | 2,111 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#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;
}
bombrary_sk