5 条题解

  • 4
    @ 2024-11-21 11:29:58

    朱昊大王美味鲜 晒足一百八十天

    • @ 2024-11-21 11:47:11

      朱昊大王美味鲜 晒足一百八十天

    • @ 2024-11-23 8:44:05

      朱昊大王美味鲜 晒足一百八十天

  • 1
    @ 2024-11-23 8:47:38
    def convert(s1):     
        st=[0]*100
        top=-1
        s2="" 
        a=""
        for i in range(len(s1)):
            if '0'<=s1[i]<='9':
                a+=s1[i]
            else:
                if '0'<=s1[i-1]<='9':
                    s2=s2+a+" "
                    a=""
                if s1[i]=="(":
                    top+=1
                    st[top]=s1[i]
                else:
                    while top>-1 and yxj[s1[i]]<=yxj[st[top]]:
                        s2=s2+st[top]+" "
                        top-=1
                    if s1[i]==")":
                        top-=1
                    else:
                        top+=1
                        st[top]=s1[i]
        s2=s2+a+" "
        while top>-1:
            s2=s2+st[top]+" "
            top-=1
        return s2
    yxj={"+":2,"-":2,"*":3,"/":3,"(":0,")":1}  
    s1=input()
    print(convert(s1))
    
    • 1
      @ 2024-3-11 16:26:35

      Python

      from typing import Dict, List
      from re import findall
      
      instr: List[str] =  findall(r'\d+|\D', input())
      # 使用re正则表达式匹配数字和运算符
      # 例如 3+4*5-8/2 到 [3, '+', 4, '*', 45, '-', 8, '/', 2]
      # 比循环遍历方便多了
      
      precedence: Dict[str, int] = {'+': 1, '-': 1, '*': 2, '/': 2, '(': 0}
      # 定于运算符优先级字典
      stack: List[str] = []
      output: List[str] = []
      
      for token in instr:
          if token.isdigit():
              output.append(token)
          elif token == '(':
              stack.append(token)
          elif token == ')':
              while stack and stack[-1] != '(':
                  output.append(stack.pop())
                  # 压入括号内运算符
              stack.pop()
          elif token in precedence:
              while stack and precedence.get(stack[-1], 0) >= precedence[token]:
                  output.append(stack.pop())
                  # 根据优先级压入运算符
              stack.append(token)
      
      while stack:
          output.append(stack.pop())
      
      print(' '.join(output))
      
      • 0
        @ 2023-11-11 19:28:22
        def zenslis(lis):
            ops = '()*/+-';s='';lis2 = []
            for i in lis:
                if '0' <= i <= '9':
                    s += i
                    num = True
                elif i in ops:
                    if num == True:
                        lis2.append(s)
                        s=''
                        num = False
                    lis2.append(i)
            if s:
                lis2.append(s)
            return lis2
        
        def intpost(expr):
            ops = {'(':0,')':0,'**':1,'*':2,'/':2,'+':3,'-':3}
            stack = []
            post = []
            for z in expr:
                if z not in ops:
                    post.append(z)
                else:
                    if z != ')' and (not stack or z == '(' or stack[-1] == '(' or ops[z] < ops[stack[-1]]):
                        stack.append(z)
                    elif z == ')':
                        while True:
                            x = stack.pop()
                            if x != '(':
                                post.append(x)
                            else:
                                break
                    else:
                        while True:
                            if stack and stack[-1] != '(' and ops[z] >= ops[stack[-1]]:
                                post.append(stack.pop())
                            else:
                                stack.append(z)
                                break
                #print(stack)
            while stack:
                post.append(stack.pop())
            return post
        

        运行代码

        if __name__ == '__main__':
            s = input()
            lis = zenslis(s)
            post = intpost(lis)
            print(' '.join(post))
        
        • @ 2023-11-22 11:40:19

          在发题解之前请先认真阅读题目= = 题目并不要求输出中缀表达式计算结果

      • -1
        @ 2024-11-21 11:53:21

        def convert(s1):

        st=[0]*100

        top=-1

        s1=s1+']'

        s2=""

        t=0

        for i in range(len(s1)):

        if '0'<=s1[i]<='9':

        t=10*t+int(s1[i])

        else:

        if '0'<=s1[i-1]<='9':

        s2=s2+str(t)+' '

        t=0

        if s1[i]=="(":

        top+=1

        st[top]=s1[i]

        elif s1[i] in '+-*/)':

        while top>-1 and yxj[st[top]]>=yxj[s1[i]]:

        s2=s2+st[top]+' '

        top-=1

        if s1[i]==")":

        top-=1

        else:

        top+=1

        st[top]=s1[i]

        while top>-1:

        s2=s2+st[top]+" "
        
                top-=1
        

        return s2

        yxj={"+":2,"-":2,"*":3,"/":3,"(":0,")":1}

        sz=input()

        print(convert(sz))

        • 1

        信息

        ID
        800
        时间
        1000ms
        内存
        256MiB
        难度
        7
        标签
        (无)
        递交数
        840
        已通过
        167
        上传者