結果
問題 | No.708 (+ー)の式 |
ユーザー | TANIGUCHI Kousuke |
提出日時 | 2019-12-20 12:23:50 |
言語 | Ruby (3.3.0) |
結果 |
AC
|
実行時間 | 86 ms / 2,000 ms |
コード長 | 579 bytes |
コンパイル時間 | 379 ms |
コンパイル使用メモリ | 11,560 KB |
実行使用メモリ | 15,336 KB |
最終ジャッジ日時 | 2023-09-22 10:55:22 |
合計ジャッジ時間 | 2,523 ms |
ジャッジサーバーID (参考情報) |
judge14 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 82 ms
15,304 KB |
testcase_01 | AC | 82 ms
15,056 KB |
testcase_02 | AC | 86 ms
15,308 KB |
testcase_03 | AC | 81 ms
15,048 KB |
testcase_04 | AC | 82 ms
15,280 KB |
testcase_05 | AC | 82 ms
15,048 KB |
testcase_06 | AC | 82 ms
15,276 KB |
testcase_07 | AC | 82 ms
15,316 KB |
testcase_08 | AC | 83 ms
15,304 KB |
testcase_09 | AC | 82 ms
15,128 KB |
testcase_10 | AC | 84 ms
15,336 KB |
testcase_11 | AC | 82 ms
15,164 KB |
testcase_12 | AC | 81 ms
15,092 KB |
testcase_13 | AC | 83 ms
15,312 KB |
testcase_14 | AC | 82 ms
15,048 KB |
コンパイルメッセージ
Syntax OK
ソースコード
def parse(s) root = [] stack = [root] s.scan(/\d+|\D/).each do |t| if t == '(' stack << [] elsif t == ')' st = stack.pop stack.last << st elsif t == '+' stack.last << '+' elsif t == '-' stack.last << '-' else stack.last << t.to_i end end root end def evaluate(ast) return ast if ast.instance_of?(Integer) ast.each_with_index.inject(0) do |s,(t,i)| next s if t.instance_of?(String) (i == 0 || ast[i - 1] == '+') ? (s + evaluate(t)) : (s - evaluate(t)) end end puts evaluate(parse(gets.chomp))