19.火星文计算--OD

# 
# 火星文使用运算符为#$
# 其与地球人的等价公式如下
# x#y =2*x+3*y+4
# x$y =3*x +y +2
# x y 是无符号整数
# 地球人公式按照c语言规则进行计算
# 火星人公式按照c语言规则进行计算,火星人公式中$符号优先级高于#相同的运算符从左到右的顺序元算
# 
# 输入
# 火星人字符串表达式结尾不带回车换行
# 输入的字符串说明是仅有无符号整数和操作符组成的计算表达式
# 1.用例保证字符串中操作数与操作符之间没有任何分隔符
# 2.用例保证操作数取值范围为32位无符号整数
# 3.保证输入以及计算结果不会出现整型溢出
# 4.保证输入字符串为合法报文
# 
# 
# 输入 : 7#6$5#12
# 输出: 226



import re


def sharp(x, y):
    return 2 * x + 3 * y + 4


def dollar(x, y):
    return 3 * x + y + 2


def solveMethod(line):
    operators = re.findall(r'\W+', line)
    print('operators', operators)
    nums = list(map(int, re.findall(r'\d+', line)))
    print('nums', nums)
    pos = operators.index('$')
    print('pos', pos)
    while pos != -1:
        tmp = dollar(nums[pos], nums[pos + 1])
        nums[pos] = tmp
        nums.pop(pos + 1)
        operators.pop(pos)
        pos = operators.index('$') if '$' in operators else -1
    res = nums[0]
    for i in range(1, len(nums)):
        res = sharp(res, nums[i])
    print(res)


if __name__ == '__main__':
    line = input().strip()
    solveMethod(line)


  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值