输出一行,包含两个整数a和b(a和b均为32位整数)和一个运算符,运算符为“+”,“-”,“*”,"\"中的一个。(数据保证不会出现除0的情况)
输出一个整数,为上述表达式计算出的结果。
2 * 4
8
5 / 4
1
3 + 23
26
时间复杂度,额外空间复杂度
。
测试错误的数据
输入:363213591 * -850993220
结果:265850340
Python代码如下:
def add(x,y):
while(y):
temp=x^y
y=(x&y)<<1
x=temp
return x
def negative(x):
return add(~x,1)
def subtraction(x,y):
if(x<y):
y=negative(y)
return add(x,y)
elif(x==y):
return 0
else:
x=negative(x)
return negative(add(x,y))
def multiply(x,y):
c=1 if(x^y<0) else 0
x=negative(x) if(x<0) else x
y=negative(y) if(y<0) else y
temp=0
while(y):
if(y&1):
temp=add(temp,x)
x<<=1
y>>=1
temp=negative(temp) if(c) else temp
return temp
def division(x,y):
if(x==0 and y!=0):
return 0
c=1 if(x^y<0) else 0
x=negative(x) if(x<0) else x
y=negative(y) if(y<0) else y
if(x<y and y!=0):
return 0
temp=0
while(x>=y):
trial=y
t=1
while((trial<<1)<=x and (trial<<1)>0):
t<<=1
trial<<=1
temp=add(temp,t)
x=subtraction(x,trial)
temp=negative(temp) if(c) else temp
return temp
ls=input().strip().split()
x=int(ls[0])
y=int(ls[2])
o=ls[1]
if '+' in o:
print(add(x,y))
elif '-' in o:
print(subtraction(x,y))
elif '*' in o:
print(multiply(x,y))
elif '/' in o or '\\' in o:
print(division(x,y))