华为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))
          

全部评论
楼主是什么岗位
点赞 回复 分享
发布于 2023-05-07 09:42 青海
加一加一,到死都只有50%
点赞 回复 分享
发布于 2023-05-06 21:34 广东

相关推荐

点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客企业服务