結果

問題 No.49 算数の宿題
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-03 19:58:04
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 655 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 10,796 KB
実行使用メモリ 7,928 KB
最終ジャッジ日時 2023-08-24 17:26:32
合計ジャッジ時間 819 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,792 KB
testcase_01 AC 16 ms
7,800 KB
testcase_02 AC 16 ms
7,792 KB
testcase_03 AC 16 ms
7,852 KB
testcase_04 AC 15 ms
7,892 KB
testcase_05 AC 15 ms
7,928 KB
testcase_06 AC 15 ms
7,888 KB
testcase_07 AC 15 ms
7,816 KB
testcase_08 AC 15 ms
7,828 KB
testcase_09 AC 14 ms
7,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

S = input()

def parse(S):
    operators = set(['*', '+'])
    nums = []
    ops = []
    tmp = 0
    for s in S:
        if s in operators:
            nums.append(tmp)
            tmp = 0
            ops.append(s)
        else:
            tmp *= 10
            tmp += int(s)
    nums.append(tmp)
    return nums, ops

def calc(S):
    nums, ops = parse(S)
    if len(ops) == 0:
        return nums[0]
    val = nums[0]
    for op, arg in zip(ops, nums[1:]):
        if op == '+':
            val *= arg
        elif op == '*':
            val += arg
        else:
            raise RuntimeError('Unknown operator!' + op)
    return val

print(calc(S))
0