题解 | #四则运算#
四则运算
https://www.nowcoder.com/practice/9999764a61484d819056f807d2a91f1e
import math def jjcc(s): while '*' in s or '/' in s: for i in s: if i=='*': tmp=float(s[s.index(i)-1])*float(s[s.index(i)+1]) s=s[:s.index(i)-1]+[tmp]+s[s.index(i)+2:] break elif i=='/': tmp=float(s[s.index(i)-1])/float(s[s.index(i)+1]) s=s[:s.index(i)-1]+[tmp]+s[s.index(i)+2:] break while '+' in s or '-' in s: for i in s: if i=='+': tmp=float(s[s.index(i)-1])+float(s[s.index(i)+1]) s=s[:s.index(i)-1]+[tmp]+s[s.index(i)+2:] break elif i=='-': if s.index('-') !=0: tmp=float(s[s.index(i)-1])-float(s[s.index(i)+1]) s=s[:s.index(i)-1]+[tmp]+s[s.index(i)+2:] else: s=[-1*s[1]]+s[2:] break return s t=''.join([' '+i+' ' if not i.isnumeric() else i for i in input()]) t=t.split() while '(' in t or '[' in t or '{' in t: for i in t[::-1]: if i=='(': idx1=len(t)-1-t[::-1].index('(') idx2 =idx1+1+t[idx1+1:].index(')') tmp=jjcc(t[idx1+1:idx2]) t=t[:idx1]+tmp+t[idx2+1:] elif i =='{': idx1 = len(t) - 1 - t[::-1].index('{') idx2 =idx1+1+t[idx1+1:].index('}') tmp=jjcc(t[idx1+1:idx2]) t = t[:idx1] + tmp + t[idx2 + 1:] elif i =='[': idx1 = len(t) - 1 - t[::-1].index('[') idx2 =idx1+1+t[idx1+1:].index(']') tmp=jjcc(t[idx1+1:idx2]) t = t[:idx1] + tmp + t[idx2 + 1:] print(round(jjcc(t)[0]))