結果

問題 No.49 算数の宿題
ユーザー tshigtshig
提出日時 2016-08-18 10:47:26
言語 Ruby
(3.3.0)
結果
AC  
実行時間 88 ms / 5,000 ms
コード長 639 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 11,452 KB
実行使用メモリ 15,348 KB
最終ジャッジ日時 2023-08-24 17:33:46
合計ジャッジ時間 1,628 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
15,144 KB
testcase_01 AC 88 ms
15,348 KB
testcase_02 AC 81 ms
15,036 KB
testcase_03 AC 84 ms
15,136 KB
testcase_04 AC 80 ms
15,160 KB
testcase_05 AC 79 ms
15,032 KB
testcase_06 AC 80 ms
15,152 KB
testcase_07 AC 81 ms
15,032 KB
testcase_08 AC 81 ms
15,168 KB
testcase_09 AC 80 ms
15,060 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class Calc0049
  def initialize(args)
    args = args.map { |l| l.chomp.split(/\s+/) }
    @s = args.shift.first
  end

  def run
    stack = []
    @s.scan(/\d+|[+*]/).each do |t|
      case t
      when /\d+/
        t = t.to_i
        if stack.empty?
          stack.push(t)
        else
          op = stack.pop
          target = stack.pop
          case op
          when '+'
            stack.push(target * t)
          when '*'
            stack.push(target + t)
          end
        end
      when '+', '*'
        stack.push(t)
      end
    end
    stack.pop
  end
end

puts Calc0049.new(STDIN.readlines).run if __FILE__ == $0
0