結果

問題 No.193 筒の数式
ユーザー data9824data9824
提出日時 2015-05-22 03:13:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,287 bytes
コンパイル時間 468 ms
コンパイル使用メモリ 61,736 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 08:44:07
合計ジャッジ時間 1,400 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>
#include <limits>
#include <algorithm>

using namespace std;

int evaluate(const string& s) {
	enum State {
		STATE_OPERATOR,
		STATE_NUMBER
	};
	State state = STATE_OPERATOR;
	int result = 0;
	char lastOperator = 0;
	int number = 0;
	for (size_t i = 0; i <= s.size(); ++i) {
		char ch = s.c_str()[i];
		switch (state) {
		case STATE_OPERATOR:
			if (isdigit(ch)) {
				number *= 10;
				number += (int)(ch - '0');
				state = STATE_NUMBER;
			} else {
				return numeric_limits<int>::min();
			}
			break;
		case STATE_NUMBER:
			if (isdigit(ch)) {
				number *= 10;
				number += (int)(ch - '0');
			} else if (ch == '+' || ch == '-') {
				result += number * (lastOperator == '-' ? -1 : 1);
				number = 0;
				lastOperator = ch;
				state = STATE_OPERATOR;
			} else if (ch == 0) {
				result += number * (lastOperator == '-' ? -1 : 1);
				return result;
			} else {
				return numeric_limits<int>::min();
			}
			break;
		}
	}
	return numeric_limits<int>::min();
}

int main() {
	string s;
	cin >> s;
	int result = numeric_limits<int>::min();
	for (size_t start = 0; start < s.size(); ++start) {
		string expression = s.substr(start) + s.substr(0, start);
		result = max(result, evaluate(expression));
	}
	cout << result << endl;
	return 0;
}
0