結果

問題 No.708 (+ー)の式
ユーザー lam6er
提出日時 2025-03-20 21:15:52
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,026 bytes
コンパイル時間 174 ms
コンパイル使用メモリ 82,600 KB
実行使用メモリ 67,324 KB
最終ジャッジ日時 2025-03-20 21:16:38
合計ジャッジ時間 1,565 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 RE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

def evaluate(s):
    # Process all parentheses
    while ')' in s:
        end = s.find(')')
        start = s.rfind('(', 0, end)
        sub_expr = s[start+1:end]
        sub_val = calculate(sub_expr)
        s = s[:start] + str(sub_val) + s[end+1:]
    # Now evaluate the expression without parentheses
    return calculate(s)

def calculate(s):
    if not s:
        return 0
    i = 0
    n = len(s)
    # Process the first number
    sign = 1
    if i < n and s[i] in '+-':
        sign = -1 if s[i] == '-' else 1
        i += 1
    current = sign * int(s[i])
    i += 1
    # Process subsequent operators and numbers
    while i < n:
        op = s[i]
        i += 1
        num_sign = 1
        if i < n and s[i] in '+-':
            num_sign = -1 if s[i] == '-' else 1
            i += 1
        current = current + num_sign * int(s[i]) if op == '+' else current - num_sign * int(s[i])
        i += 1
    return current

def main():
    s = input().strip()
    print(evaluate(s))

if __name__ == "__main__":
    main()
0