华为5.6第二题
题目计算输入的表达式值,表达式中含有特殊符号!@#,需要单独计算。
我把两个数据分成整数和小数部分计算的,但最后只能通过50%,有大佬能帮我看看是为什么吗?
# If you need to import additional packages or classes, please import here.
def func():
l=int(input())
a,b=input().split('+')
d={"!":0,"@":1,"#":2}
v=[[0,13,4],[13,7,20],[4,20,5]]
ans=0
if ('.'in a):
ah,al=a.split('.')
else:
ah=a
al='0'
if '.'in b:
bh,bl=b.split('.')
else:
bh=b
bl='0'
#print(ah,al,bh,bl)
ansl=''
ansh=''
#计算小数部分
ll=max(len(al),len(bl))
temp=0
flag=0
if ll>len(al):
al=al+'0'*(ll-len(al))
if ll>len(bl):
bl=bl+'0'*(ll-len(bl))
#print(al,bl)
for i in range(ll):
ca=al[len(al)-1-i]
cb=bl[len(bl)-1-i]
#print(ca,cb)
if ca in ['!','@','#']:
tempval=v[d[ca]][d[cb]]
else:
tempval=int(ca)+int(cb)
tempval=tempval+temp
temp=0
if(tempval>=10):
temp=tempval//10
tempval%=10
if tempval!=0:
flag=1
if flag==0 and tempval==0:
continue
ansl=str(tempval)+ansl
#计算整数部分,temp保留小数部分的进位
lh=max(len(ah),len(bh))
if lh>len(ah):
ah='0'*(lh-len(ah))+ah
if lh>len(bh):
bh='0'*(lh-len(bh))+bh
for i in range(lh):
ca=ah[len(ah)-1-i]
cb=bh[len(bh)-1-i]
if ca in ['!','@','#']:
tempval=v[d[ca]][d[cb]]
else:
tempval=int(ca)+int(cb)
tempval=tempval+temp
temp=0
if(tempval>=10):
temp=tempval//10
tempval%=10
ansh=str(tempval)+ansh
if temp!=0:
ansh=str(temp)+ansh
#print(ansl)
#print(ansh)
if (ansl=='' or ansl=='0') and (ansh=='' or ansh=='0'):
print(0)
elif ansl=='' or ansl=='0':
print(int(ansh))
elif ansh=='' or ansh=='0':
print(float('0.'+ansl))
else:
print(float(ansh+'.'+ansl))
