1 条题解

  • 1
    @ 2022-4-24 9:51:49

    python 3:

    def calculate(s: str) -> int:
            
        def helper(s: list) -> int:
            stack = []
            sign = '+'
            num = 0
    
            while len(s) > 0:
                c = s.pop(0)
                if c.isdigit():
                    num = 10 * num + int(c)
                if c == '(':
                    num = helper(s)
    
                if (not c.isdigit() and c != ' ') or len(s) == 0:
                    if sign == '+':
                        stack.append(num)
                    elif sign == '-':
                        stack.append(-num)
                    elif sign == '*':
                        stack[-1] = stack[-1] * num
                    elif sign == '/':
                        stack[-1] = int(stack[-1] / float(num))  
                    elif sign == '^':
                        stack[-1] = int(stack[-1]**num)
                    num = 0
                    sign = c
                if c == ')': break
            return sum(stack)
    
        return helper(list(s))
    
    a=input()
    print(calculate(a))
    • 1

    信息

    ID
    62
    时间
    1000ms
    内存
    128MiB
    难度
    5
    标签
    递交数
    67
    已通过
    25
    上传者