結果

問題 No.49 算数の宿題
ユーザー antaanta
提出日時 2014-10-23 22:44:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,564 bytes
コンパイル時間 709 ms
コンパイル使用メモリ 77,760 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 17:22:22
合計ジャッジ時間 1,404 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long, long long> pll; typedef vector<pair<long long, long long> > vpll;
typedef vector<string> vs; typedef long double ld;
template<typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; }


namespace parse {
	typedef const char *Pos;

	struct ParseError {
		Pos p_;
		std::stringstream *ss_;
		ParseError(const Pos &p) : p_(p), ss_(new std::stringstream()) { }
		ParseError(const ParseError &that) : p_(0), ss_(0) { *this = that; }
		ParseError &operator=(const ParseError &that) {
			delete ss_;
			p_ = that.p_;
			ss_ = new std::stringstream(that.ss_->str());
			return *this;
		}
		~ParseError() { delete ss_; }
		template<typename T> ParseError &operator<<(const T &t) {
			*ss_ << t;
			return *this;
		}
		friend std::ostream &operator<<(std::ostream &o, const ParseError &e) {
			o << e.ss_->str() << " at: ";
			Pos q = e.p_;
			for (int k = 0; *q && k < 20; ++q, ++k)
				o << *q;
			if (*q) o << "...";
			else o << "(end of input)";
			return o;
		}
	};

	inline bool cond(bool b, Pos &p) {
		if (b) ++p;
		return b;
	}

	inline bool rewind(int k, bool b, Pos &p) {
		if (!b) p -= k;
		return b;
	}
	inline bool rw0(bool b, Pos &p) { return b; }
	inline bool rw1(bool b, Pos &p) { return rewind(1, b, p); }
	inline bool rw2(bool b, Pos &p) { return rewind(2, b, p); }

	inline bool optional(bool) { return true; }

	inline bool expect(bool b, const Pos &p) {
		if (!b) throw ParseError(p) << "parse error";
		return b;
	}
	inline bool char_(char c, Pos &p) {
		return cond(c == *p, p);
	}

	inline bool string_(const char *str, Pos &p) {
		Pos o = p;
		for (const char *s = str; *s; ++s, ++p) {
			if (*s != *p) {
				p = o;
				return false;
			}
		}
		return true;
	}
};
using namespace parse;

bool natural(Pos &p, int &res) {
	if (!isdigit(*p)) return false;
	int c = 0;
	while (isdigit(*p)) {
		c = c * 10 + (*p - '0');
		++p;
	}
	res = c;
	return true;
}

bool expr(Pos &p, int &res) {
	if(!natural(p, res)) return false;
	while (*p == '+' || *p == '*') {
		char op = *p;
		++p;
		int operand;
		expect(natural(p, operand), p);
		if (op == '+')
			res *= operand;
		else
			res += operand;
	}
	return true;
}


int main() {
	char *S = new char[101];
	scanf("%s", S);
	Pos p = S;
	int res;
	expr(p, res);
	cout << res << endl;
	return 0;
}
0