結果

問題 No.1135 RPN
ユーザー damin
提出日時 2020-11-04 01:44:44
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
RE  
実行時間 -
コード長 1,117 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-07-22 09:34:07
合計ジャッジ時間 1,531 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14 WA * 1 RE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
try:
    import os
    f = open('input.txt', 'r')
    sys.stdin = f
except FileNotFoundError:
    None
from math import sqrt, ceil
input=lambda: sys.stdin.readline().strip()

n=int(input())
A=list(map(str,input().split()))
s=A
now=0

def expr():
    global now
    ans= term()
    while now<n and s[now] not in "+-" :
        y =  expr()
        op = s[now]
        now+=1
        if op =="+":
            ans+= y
        else:
            ans-=y
    return ans


def term():
    global now
    if now >=n:
        return 0
    elif n-2 <= now:
        # print(s[now],now)
        x = int(s[now])
        now +=1
        return x
    elif s[now].isdecimal() and s[now+1].isdecimal() and s[now+2] in "+-":
         x = int(s[now])
         y = int(s[now+1])
         op = s[now+2]
         now+=3
         if op=="+": return x+y
         return x-y
    elif all([s[i].isdecimal() for i in range(now,now+3)]):
        x = int(s[now]); now+=1
        y = term()
        op = s[now]
        now+=1
        if op=="+":return x+y
        return x-y
        # now+=1
        # return int(s[now])



print(expr())
0