結果

問題 No.1135 RPN
ユーザー damindamin
提出日時 2020-11-04 02:56:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 2,425 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 10,756 KB
実行使用メモリ 8,468 KB
最終ジャッジ日時 2023-09-29 15:27:56
合計ジャッジ時間 1,423 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
8,468 KB
testcase_01 AC 13 ms
7,964 KB
testcase_02 AC 13 ms
8,268 KB
testcase_03 AC 13 ms
8,444 KB
testcase_04 AC 14 ms
8,424 KB
testcase_05 AC 14 ms
7,960 KB
testcase_06 AC 14 ms
8,420 KB
testcase_07 AC 12 ms
7,964 KB
testcase_08 AC 13 ms
8,020 KB
testcase_09 AC 14 ms
8,352 KB
testcase_10 AC 13 ms
8,440 KB
testcase_11 AC 17 ms
7,980 KB
testcase_12 AC 13 ms
8,408 KB
testcase_13 AC 13 ms
8,300 KB
testcase_14 AC 13 ms
8,388 KB
testcase_15 AC 13 ms
7,964 KB
testcase_16 AC 13 ms
8,076 KB
testcase_17 AC 13 ms
7,968 KB
testcase_18 AC 13 ms
8,104 KB
権限があれば一括ダウンロードができます

ソースコード

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())
s=list(map(str,input().split()))

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)]):
#     else:
#
#         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())


def expr():
    global now
    x = term()
    ans = x
    while now<n and s[now].isdecimal():
        y = expr()
        op = s[now] ;now+=1
        # print(x,y,op,now)
        if op =="+":ans +=y
        else: ans-=y
    return ans

# def term():
#     global now
#     if n-2 <= now:
#         now+=1
#         return int(s[now-1])
#     if s[now].isdecimal() and s[now+1].isdecimal() and s[now+2] in "+-":
#         op= s[now+2]
#         now+=3
#         if op=="+":
#             return int(s[now-3])+ int(s[now-2])
#         else:
#             return int(s[now-3])-int(s[now-2])
#     else:
#         now+=1
#         return int(s[now-1])

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
         # print("term-d",x,y,op,now)
         if op=="+": return x+y
         return x-y
    # elif all([s[i].isdecimal() for i in range(now,now+3)]):
    else:
        x = int(s[now])
        now+=1
        return x

print(expr())
0