結果

問題 No.193 筒の数式
ユーザー くれちーくれちー
提出日時 2017-02-04 19:31:31
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 1 ms / 1,000 ms
コード長 1,202 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 25,744 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-25 20:13:50
合計ジャッジ時間 1,041 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define rep(i, n) for (i = 0; i < n; i++)
#define rrep(i, n) for (i = n; i >= 0; i--)
#define max(a, b) (a > b ? a : b)
#define inf 1145141919
#define minus 0
#define plus 1

bool is_operator(char c) {
	if (c == '+' || c == '-') return true;
	else return false;
}

bool is_calcable(char *s, int l) {
	if (is_operator(s[0]) || is_operator(s[l - 1])) return false;
	int i; rep(i, l - 1)
		if (is_operator(s[i]) && is_operator(s[i + 1])) return false;
	return true;
}

int calc(char *s, int l) {
	int i, ti = 0, oi = 0, t[5] = {}, o[5] = {}, ans = 0;
	rep(i, l) {
		while (!is_operator(s[i]) && i < l) {
			t[ti] *= 10;
			t[ti] += s[i] - '0';
			i++;
		}
		if (i < l) o[oi] = s[i] == '+' ? plus : minus;
		ti++; oi++;
	}
	ans = t[0];
	rep(i, oi - 1) {
		if (o[i] == plus) ans += t[i + 1];
		else ans -= t[i + 1];
	}
	return ans;
}

void rotate(char *s, int l) {
	char t = s[l - 1];
	int i; rrep(i, l - 1) s[i + 1] = s[i];
	s[0] = t;
	return;
}

int main() {
	char s[11];
	scanf("%s", s);

	int i, ans = -inf, l = strlen(s);
	rep(i, l) {
		if (is_calcable(s, l)) ans = max(ans, calc(s, l));
		rotate(s, l);
	}

	printf("%d\n", ans);
	return 0;
}
0