题解 | #简单表达式计算#

简单表达式计算

https://www.nowcoder.com/practice/6221faa383fc49f1b10dffcb62c866bf

# 读入表达式,调用eval函数,三行搞定。
import sys
inp = sys.stdin.read().strip().split("\n")[:-1]
for s in inp: print(eval(s))


# def calc(a, b, op):
#     # print(a,b,op)
#     if '+'==op: return a+b
#     if '-'==op: return a-b
#     if '*'==op: return a*b
#     assert("error")

# op = set('+-*')
# for s in inp:
#     # print(eval(s))
#     stack0 = []
#     stack1 = []
#     i = 0
#     while i<len(s):
#         if s[i] not in op:
#             j=i+1
#             while j<len(s) and s[j] not in op: j+=1
#             stack0.append(int(s[i:j]))
#             i = j
#         else: 
#             o = s[i]
#             if o=="*": stack1.append(o)
#             else:
#                 while len(stack1):
#                     b, a = stack0.pop(), stack0.pop()
#                     c = calc(a,b,stack1.pop())
#                     stack0.append(c)
#                 stack1.append(o)
#             i+=1

#     while len(stack1):
#         b, a = stack0.pop(), stack0.pop()
#         c = calc(a,b,stack1.pop())
#         stack0.append(c)
#     print(stack0[0])


全部评论

相关推荐

迷茫的大四🐶:💐孝子启动失败,改为启动咏鹅
点赞 评论 收藏
分享
10-20 15:26
门头沟学院 Java
桥头牛油火锅:这个比例不正常,简历的话项目经历放中间,项目功能分点可以再明确点,前面加“·”或者“1 2 3”,另外简历上的照片可以去外面摄影店拍一下,以后也会用到的,hr筛人也是多少会看的,毕竟世界是一个巨大的卡颜局嘛,还有有些hr由于消息太多可能没看到,后面可能会回来找你,要简历的还会多一点,我也是普2本,比例大致是600:90:15:3,当然我实力不太够,拿的offer比较少,慢慢来吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务